Results 1 to 4 of 4
Thread: nextLine issue
- 03-24-2011, 08:30 PM #1
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.");
- 03-24-2011, 10:57 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 94
- Rep Power
- 0
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());
- 03-24-2011, 11:16 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 12
- Rep Power
- 0
try to use next(); instead of nextLine();
best regards,
jed
- 03-24-2011, 11:48 PM #4
Similar Threads
-
Scanner - obtaining strings from console with nextLine
By codeAJ in forum New To JavaReplies: 3Last Post: 03-23-2011, 11:16 AM -
input nextLine error
By scoobyrox in forum New To JavaReplies: 6Last Post: 02-25-2011, 01:08 AM -
Problem Of Scanner Object with its method nextLine()
By Cluster Storm in forum AWT / SwingReplies: 12Last Post: 06-17-2010, 05:40 PM -
input.nextLine();
By Cass29 in forum New To JavaReplies: 3Last Post: 12-21-2009, 07:43 PM -
.nextLine(); only picks up first word
By ethanemc505 in forum New To JavaReplies: 1Last Post: 10-08-2009, 07:30 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks