This is frusturating me! *JAVA NOOB*

Discussion in 'OT Technology' started by cheet, Apr 17, 2006.

  1. cheet

    cheet OT Supporter

    Joined:
    Mar 22, 2004
    Messages:
    13,235
    Likes Received:
    3
    Location:
    CA
    This is a program that maintains an ArrayList of employees and does functions like add employee, remove employee, list employees, and compute
    their paycheck. There is a menu where this method, addEmployee(), is called.

    For some reason after I input the first employee, whenever I add additional employees it skips asking me for their name. It displays "Enter Employee Name" but then immediately goes to "Enter employee type..." and waits for an int input there.

    I can continue to add employees but they all have no name and just a wage.

    What am I doing wrong that makes it skip asking for a name?

    Code:
     
    private void addEmployee()
     {
      System.out.println("Enter Employee Name: ");
      String name = scanner.nextLine();
      System.out.println("Enter Employee Type (1 = Hourly, 2 = Contract, 3 = Salary: ");
      int t = scanner.nextInt();
      int wage = 0;
      System.out.println();
       switch (t)
       {
       case 1:
        System.out.println("Enter Hourly Wage (in cents): ");
        wage = scanner.nextInt();
        employees.add(new HourlyEmployee(name, wage));
        System.out.println("Employee entered!");
        break;
       case 2:
        System.out.println("Enter Hourly Wage (in cents): ");
        wage = scanner.nextInt();
        employees.add(new ContractEmployee(name, wage));
        System.out.println("Employee entered!");
        break;
       case 3:
        System.out.println("Enter Annual Salary (in cents): ");
        wage = scanner.nextInt();
        employees.add(new SalaryEmployee(name, wage));
        System.out.println("Employee entered!");
        break;
       default:
        System.out.println("INVALID ENTRY, Please try again.");
        break;
       }
      go();
     } 
    
    go() calls the main menu, BTW
     
  2. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    Your scanner object probably has a newline character left in its buffer after the previous call to scanner.nextLine(). Try flushing it at the top of the method.
     
  3. HS-Gravtron

    HS-Gravtron Mmmmmm

    Joined:
    Oct 13, 2002
    Messages:
    167
    Likes Received:
    0
    Yeah, make sure scanner's buffer is empty. Check to see if the name is actually blank, or if it is a new line character.

    Its an issue with scanner, but we'd need the code or documentation for it to be able to help.
     

Share This Page