Results 1 to 4 of 4
Thread: while loop skips code
- 02-28-2009, 02:42 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 13
- Rep Power
- 0
while loop skips code
I am trying to create my first while loop and I am close, but when I run the code it skips the first line of code in the while statement. The first run through works, but when it loops through the second time it skips the first line of code and seems to enter null automatically.
Line being skipped:
System.out.println("Enter Employee Name"); //prompt
employeeName = input.nextLine(); //read first number from user
Here is my code:
int stop=8;
while(stop <= 10) {
System.out.println("Enter Employee Name"); //prompt
employeeName = input.nextLine(); //read first number from user
System.out.print( "Enter Hours Worked: " ); // prompt
hoursWorked = input.nextDouble(); // read second number from user
System.out.print( "Enter Hourly Rate: " ); // prompt
hourlyRate = input.nextDouble(); // read second number from user
sum = hourlyRate*hoursWorked;
System.out.println(employeeName + " $" + sum );
stop = stop + 1;
}
-
Your while loop may be fine. Scanner is somewhat persnickety about end of line char, and you may need to tell scanner to swallow this char before it moves on. Consider having a call to
at the bottom of your while loop. That may help.Java Code:input.nextLine();
Last edited by Fubarable; 02-28-2009 at 02:55 AM.
- 02-28-2009, 03:01 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
It's a good idea to use the code tags to ensure that your code is readable: highlight the code when you post and click the code button (labelled #).
Let's assume input is an instance of Scanner. You use a couple of nextXXX() methods and it's worth checking with the documentation to be absolutely sure of what they are doing.
To see what happens if this section of code executes repeatedly, think about where the scanner ends up. As noted in the code it will end up pointing just past the last double that was entered. When the code executes again, nextLine() will again advance "past the current line and return the input" but this input will be the empty string. Ie, there is nothing to return at the end of the line containing the previous double.Java Code:int stop=8; while(stop <= 10) { System.out.println("Enter Employee Name"); // "Advances this scanner past the current line and returns the input" // so now the scanner is "pointing at" the start of the next line employeeName = input.nextLine(); System.out.print( "Enter Hours Worked: " ); // "Scans the next token of the input as a double... // the scanner advances past the input that matched." // so now the scanner is "pointing at" the end of the last line to be input hoursWorked = input.nextDouble(); System.out.print( "Enter Hourly Rate: " ); // Again the scanner will scan and return the double // At this point the scanner is still at the end of the line containing // the input double hourlyRate = input.nextDouble(); sum = hourlyRate*hoursWorked; System.out.println(employeeName + " $" + sum ); stop = stop + 1; }
What you need to do to fix this is add a line after the last nextDouble() that will advance the scanner past the line it is on and make it ready to read the line that will be input next. nextLine() will do this - just throw away the returned string.
- 02-28-2009, 03:07 AM #4
Member
- Join Date
- Feb 2009
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
Need help with a loop
By ReV13 in forum New To JavaReplies: 15Last Post: 12-01-2008, 04:48 PM -
can you help me with this for loop?
By java_fun2007 in forum New To JavaReplies: 6Last Post: 12-22-2007, 10:20 AM -
A loop that doesn't loop
By MichYer in forum New To JavaReplies: 2Last Post: 07-30-2007, 08:44 AM -
While loop
By leebee in forum New To JavaReplies: 1Last Post: 07-18-2007, 03:11 PM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks