Results 1 to 4 of 4
  1. #1
    ejs7597 is offline Member
    Join Date
    Feb 2009
    Posts
    13
    Rep Power
    0

    Question 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;
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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
    Java Code:
    input.nextLine();
    at the bottom of your while loop. That may help.
    Last edited by Fubarable; 02-28-2009 at 02:55 AM.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

    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;
    }
    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.

  4. #4
    ejs7597 is offline Member
    Join Date
    Feb 2009
    Posts
    13
    Rep Power
    0

Similar Threads

  1. Need help with a loop
    By ReV13 in forum New To Java
    Replies: 15
    Last Post: 12-01-2008, 04:48 PM
  2. can you help me with this for loop?
    By java_fun2007 in forum New To Java
    Replies: 6
    Last Post: 12-22-2007, 10:20 AM
  3. A loop that doesn't loop
    By MichYer in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 08:44 AM
  4. While loop
    By leebee in forum New To Java
    Replies: 1
    Last Post: 07-18-2007, 03:11 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 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
  •