-
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.
-
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.
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;
}
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.
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.
-
Thanks
Thanks for the help. That seems to have worked.