Results 1 to 3 of 3

Thread: Loop-y Blah

  1. #1
    jakaldama is offline Member
    Join Date
    Sep 2011
    Posts
    16
    Rep Power
    0

    Default Loop-y Blah

    Say I need to type in my name - letter by letter, and then concatenate the input to form the whole name. All the while I am checking for numbers and marking for errors.

    So for my first name, entered in as a string I have it down pat and it seems to be working fine. but i can't figure out how to have each letter entered in and then join them together



    Scanner keyboard = new Scanner(System.in);
    System.out.println("What is your first name?");
    firstName = keyboard.nextLine();
    for(i=0; i<firstName.length();i++)
    {
    ch=firstName.charAt(i);
    if (!(ch>=65 && ch <=90))
    {
    if(!(ch>=97 && ch<=122))
    {
    flag=1;
    System.out.println("You've entered in an invalid name");
    break;
    }
    }
    }
    System.out.println("Enter in your last name, letter by letter");

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: Loop-y Blah

    If you have a valid firstName and a valid lastName you can join the two together as follows:

    Java Code:
    String firstName= ...;
    String lastName= ...;
    String completeName= firstName+" "+lastName;
    But your question raises another question: what are those numbers 65, 90, 97 and 122 doing in your code?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jakaldama is offline Member
    Join Date
    Sep 2011
    Posts
    16
    Rep Power
    0

    Default Re: Loop-y Blah

    Thanks for the reply, what I need to do though is enter in my last name, character by character

    s (enter)
    m(enter)
    i (enter)
    and so on

    and then add all the letters together to form the whole last name.

Similar Threads

  1. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  2. [Q] Loop issue (while loop)
    By iriscience in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 04:21 PM
  3. JAVAC is not recognized blah blah....=(
    By Lance87 in forum New To Java
    Replies: 33
    Last Post: 12-20-2010, 08:09 PM
  4. Replies: 5
    Last Post: 04-30-2010, 05:05 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •