Results 1 to 4 of 4
Thread: input.nextLine();
- 12-21-2009, 01:24 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 4
- Rep Power
- 0
input.nextLine();
Hi everyone hope someopne can help me. I'm writing a code that holds a clients details and I want to check if the right details are displayed. When I prompt the user to enter the new details I'm getting two line diplayed as one. Here is the section of code I have
public void checkDetails()
{
int correct;
Scanner input = new Scanner(System.in);
System.out.println( "Are the Details Displayed Above Correct? (Please Enter 1 for Yes and 2 for No" );
correct = input.nextInt();
System.out.println();
while( correct == 2 )
{
System.out.println("Please Enter Clients Name:");
clientName = input.nextLine();
System.out.println("Please Enter Clients Street Address:");
clientStreet = input.nextLine();
System.out.println("Please Enter Clients Town:");
clientTown = input.nextLine();
System.out.println("Please Enter Clients State:");
clientState = input.nextLine();
System.out.println("Please Enter Clients Budget:");
clientBudget = input.nextInt();
}
}
The problem is that Enter Clients Name and Enter Clients Street
comes out together without a space to enter the name so whatever I enter for name is stored as the stree address.
- 12-21-2009, 01:30 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
If you type '2' (for incorrect details) note that you actually type <2> <enter> so two chars are present in the input buffer; the nextInt() method reads the <2>, takes it from the buffer and converts it to an int but it leaves the <enter> in the input buffer (because its not part of an int representation). Next you attempt to read an entire line, that method finds the <enter> character in the input buffer and duly decides that you have typed an empty line and returns.
You have to get rid of that <enter> key in the input buffer; simply read a line by calling the nextLine() method (and discard its return value) before you want to read a client's name.
kind regards,
Jos
- 12-21-2009, 01:40 PM #3
Member
- Join Date
- Dec 2009
- Location
- Rio de Janeiro
- Posts
- 38
- Rep Power
- 0
Hi Cass29 welcome to the forum.
1. Please enter your code between the tags "code", the "#" symbol that appears when you type the text of your post. :D
2. Please avoid using nextInt(); nextDouble(); nextFloat(); etc methods because they do not recognize "enter" as part of what you wrote (since "enter" is not a number). That's a problem because "enter" still remains in buffer and when you try to read another information from the keyboard, it skips this information.
Use nextLine() instead.
Change:
Java Code:correct = input.nextInt();
Java Code:correct = Integer.parseInt(input.nextLine());
Java Code:int choice; boolean correct; Scanner input = new Scanner(System.in); System.out.println( "Are the Details Displayed Above Correct? (Please Enter 1 for Yes and 2 for No" ); choice = Integer.parseInt(input.nextLine()); correct = (choice == 2); System.out.println(); while( correct ) {
Please don't laugh at my English... I'm trying my best! :)
- 12-21-2009, 08:43 PM #4
Member
- Join Date
- Dec 2009
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
.nextLine(); only picks up first word
By ethanemc505 in forum New To JavaReplies: 1Last Post: 10-08-2009, 08:30 PM -
Input help
By rice in forum New To JavaReplies: 6Last Post: 09-26-2009, 08:07 PM -
Input technique for unknown lines of input
By ducreative in forum New To JavaReplies: 16Last Post: 09-23-2009, 10:26 AM -
Input....in JTable
By kirtesh4u in forum AWT / SwingReplies: 0Last Post: 11-15-2008, 10:15 AM -
how to take input and verify input in Java programs
By bilal_ali_java in forum Advanced JavaReplies: 0Last Post: 07-21-2007, 09:46 AM
Bookmarks