10 Replies Latest reply: Feb 23, 2007 7:48 AM by 807599 RSS

    Creating Strings

    807599
      Hello ,

      I am using the book Java Software Solutions 5th editions.
      At the end of chapter 3, I need to create a project that:

      prompts for the user's first and last name (seperately), then print a string composed of the first letter of the user's first name, followed by (no more than) the first five characters of the user's last name, followed by a random number in the range of 10 to 99.

      I am having trouble with the first part: how do I create the string ? The book has not discussed converting int values to String (not until this point anyway) and I don't know what to do..

      import java.util.Scanner;

      public class Names
      {
      public static void main (String[] args)
      {
      int firstName, lastName, num;

      Scanner scan = new Scanner (System.in);
      System.out.println ("Please enter your first name. ");
      firstName = scan.nextInt();
      System.out.println ();
      System.out.println ("Please enter your last name. ");
      lastName = scan.nextInt();

      }
      }

      that's what ive done so far. any help would be appreciated !! :-(
        • 1. Re: Creating Strings
          796447
          firstName = scan.nextInt();
          Read the API for the Scanner class instead of just throwing random method calls and hoping for a miracle.
          It should be obvious that you're not asking the user to type a number, so why you'd be tempted to invoke nextInt() is beyond me.
          http://java.sun.com/j2se/1.5.0/docs/api/index.html

          It also appears you don't know what the basic types are, if you're trying to declare firstName as an int. Please start with the tutorials.
          http://java.sun.com/docs/books/tutorial/index.html
          • 2. Re: Creating Strings
            807599
            I do know what they mean, I know int are for numbers but i've tried to declare them as strings but then scan.nextString isn't a method so what do I do ?

            That was stupid indeed, but all recent projects have been with numbers and i'm still not so clear on using strings.

            And I couldn't really understand the relevant method in your link...
            • 3. Re: Creating Strings
              807599
              Here is the API page for Scanner: http://java.sun.com/javase/6/docs/api/java/util/Scanner.html

              Look for a relevant method that returns a String.
              • 4. Re: Creating Strings
                796447
                I do know what they mean, I know int are for numbers
                but i've tried to declare them as strings but then
                scan.nextString isn't a method so what do I do ?
                sigh
                Scanner.next() returns a String
                And I couldn't really understand the relevant method
                in your link...
                It isn't rocket science. On the left pane of the page if you follow the first link, is a list of classes. Find the Scanner class. Click it. On the right pane it shows the documentation of that class.
                • 5. Re: Creating Strings
                  807599
                  Here's the complete soulution for you.

                  import java.util.Random;
                  import java.util.Scanner;

                  public class Names
                  {
                  public static void main (String[] args)
                  {
                  String firstName, lastName;
                  Random ran = new Random();
                  int num;
                  do{
                       num = ran.nextInt(100);
                  }while(num<10);
                  Scanner scan = new Scanner (System.in);
                  System.out.print ("Please enter your first name. ");
                  firstName = scan.nextLine();
                  System.out.print ("Please enter your last name. ");
                  lastName = scan.nextLine();
                  if(lastName.length()>5)
                       lastName = lastName.substring(0, 5);
                  System.out.println("This is the output: " + firstName.subSequence(0,1) + lastName + num);

                  }
                  }
                  • 6. Re: Creating Strings
                    807599
                    This part of the program:
                    do{
                        num = ran.nextInt(100);
                    }while(num<10);
                    Is running and running for me. Could it be an infinite loop? By the way,
                    I just dropped by pen and it fell up.
                    • 7. Re: Creating Strings
                      807599
                      What the loop does is continue looping unless it gets a random mumber betw 10 and 100, which is the requirement.

                      This loop goes in an infinite loop only if the generated random number is never greater than 9, and, that happens if you are the most unlucky being on the planet !!!
                      • 8. Re: Creating Strings
                        807599
                        Any reason you didn't write it as:
                        int num = 10 + ran.nextInt(90);
                        Because the loop is still running! Guess I'm just unlucky.
                        • 9. Re: Creating Strings
                          807599
                          Oh yes, that's a better n efficient way...
                          • 10. Re: Creating Strings
                            807599
                            Thanks sosododo !