Results 1 to 4 of 4

Thread: nextLine issue

  1. #1
    paul1024's Avatar
    paul1024 is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default nextLine issue

    Hello all:

    I am asking the user typical questions for an address book. I am using nextLine for strings that could have spaces in them (city, state,...).

    When the program comes up to the nextLine it does not wait for the prompt, it continues. In this part of the code it asks for the address but does not wait, instead it goes on to ask if the user is finished entering in addresses. I read the javadoc about nextLine but I still can't figure out why it's doing this.

    Java Code:
    System.out.println("Enter the first name: ");
    			addrbk.setFname(in.next());
    			
    			System.out.println("Enter the last name: ");
    			addrbk.setFname(in.next());
    			
    			System.out.println("Enter the address: ");
    			addrbk.setAddress(in.nextLine());
    			
    			
    			//now that we have all the required info, write to file
    			addrbk.writeToDisk();
    			
    			//ask the use if they are finished
    			System.out.println("Stop program? Enter 'y' to stop or 'n' to add another address.");

  2. #2
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    103
    Rep Power
    0

    Default

    I believe you need to advance to the next line before you retrieve a full line for the address:
    Java Code:
    System.out.println("Enter the first name: ");
    addrbk.setFname(in.next());
    			
    System.out.println("Enter the last name: ");
    addrbk.setFname(in.next());
    
    in.nextLine();  // <<<<<< advances the input past the current line
    
    System.out.println("Enter the address: ");
    addrbk.setAddress(in.nextLine());

  3. #3
    FlemaVerde is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    try to use next(); instead of nextLine();

    best regards,

    jed

  4. #4
    paul1024's Avatar
    paul1024 is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

Similar Threads

  1. Replies: 3
    Last Post: 03-23-2011, 12:16 PM
  2. input nextLine error
    By scoobyrox in forum New To Java
    Replies: 6
    Last Post: 02-25-2011, 02:08 AM
  3. Problem Of Scanner Object with its method nextLine()
    By Cluster Storm in forum AWT / Swing
    Replies: 12
    Last Post: 06-17-2010, 06:40 PM
  4. input.nextLine();
    By Cass29 in forum New To Java
    Replies: 3
    Last Post: 12-21-2009, 08:43 PM
  5. .nextLine(); only picks up first word
    By ethanemc505 in forum New To Java
    Replies: 1
    Last Post: 10-08-2009, 08:30 PM

Posting Permissions

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