Results 1 to 4 of 4
  1. #1
    stupidmoron is offline Member
    Join Date
    Jan 2014
    Posts
    3
    Rep Power
    0

    Question Scanner's nextLine() won't let me enter anything

    My program is supposed to let me enter the following info for 3 employees: name, number of hours worked this week, and hourly pay rate. It lets me enter the name for the first employee, but for the second and third, it skips over the name and forces me to enter the number of hours worked and pay rate without a name. I have the code and the output posted below. First the program code:

    Java Code:
    import java.util.Scanner;
    public class SalaryCalc {
      public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String name;
        double hours;
        double rate;
        double grossPay = 0;
        double overtime;
        
        for(int i=1; i<=3; i++) {
          System.out.println("\nEnter employee's name: ");
          name = input.nextLine();
          System.out.println("\nEnter hours worked this week: ");
          hours = input.nextDouble();
          System.out.println("\nEnter pay rate: ");
          rate = input.nextDouble();
          if(hours > 40.0) {
            overtime = hours - 40.0;
            grossPay = (hours * rate) + (overtime * 1.5);
          }
          else {
            grossPay = hours * rate;
          }
          System.out.printf("\n%s made $%.2f this week\n", name, grossPay);
        }  // end for
      }
    }
    Now the output:

    Java Code:
    run:
    
    Enter employee's name: 
    Bigfoot
    
    Enter hours worked this week: 
    42
    
    Enter pay rate: 
    10
    
    Bigfoot made $423.00 this week
    
    Enter employee's name: 
    
    Enter hours worked this week:
    Notice I can only enter the name for the first employee (Bigfoot). If I wanted to enter "Nessie" for my second employee's name, it skips over the name prompt and prompts me for the hours worked instead. What am I doing wrong? Any help will be greatly appreciated.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,389
    Rep Power
    5

    Default Re: Scanner's nextLine() won't let me enter anything

    There is still a lingering line terminator after you scan in the rate. So put in a nextLine after the scan to clear it. It's been a while since I did this so check the API for more info.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    stupidmoron is offline Member
    Join Date
    Jan 2014
    Posts
    3
    Rep Power
    0

    Default Re: Scanner's nextLine() won't let me enter anything

    Quote Originally Posted by jim829 View Post
    There is still a lingering line terminator after you scan in the rate. So put in a nextLine after the scan to clear it. It's been a while since I did this so check the API for more info.
    Where is it? Can you please point it out to me, in the lines below? (confused)

    Java Code:
    System.out.println("\nEnter pay rate: ");
    rate = input.nextDouble();

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: Scanner's nextLine() won't let me enter anything

    The nextDouble() and nextInt() methods do what their name says: they read a double or an int and nothing that follows it; not even a newline character; you have to get rid of that newline character before you can read another line (otherwise your code 'thinks' that it read an empty line because of that pending newline character); so after reading a double (or an int) put in a nextLine() method call to get rid of that newline character.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Scanner.nextLine ??
    By NotTooOld in forum New To Java
    Replies: 5
    Last Post: 05-07-2013, 01:26 PM
  2. Replies: 1
    Last Post: 01-30-2012, 04:41 AM
  3. That old problem with scanner and end of line (enter)
    By Java Riser in forum New To Java
    Replies: 2
    Last Post: 09-29-2011, 01:54 AM
  4. Replies: 3
    Last Post: 03-23-2011, 11:16 AM
  5. Problem Of Scanner Object with its method nextLine()
    By Cluster Storm in forum AWT / Swing
    Replies: 12
    Last Post: 06-17-2010, 05:40 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •