1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: In.nextLine(); does not work properly

  1. May 22, 2012 #1
    1. The problem statement, all variables and given/known data

    I am to design a Java program that inputs some information about employees. Then the computer displays it on the screen.

    Here is a code snippet:

    Code (Text):
    System.out.println("You will now provide information for " + numberOfEmployees +
            " employees.");
            for(int i = 0; i < numberOfEmployees; i++)
                System.out.print("Enter the employee's name: ");
                [B]tempEmployeeName[i] = in.nextLine();[/B]            
                System.out.println(tempEmployeeName[i] + "'s employee number will be " + (1000+i));
                tempEmployeeNumber[i] = 1000+(i+1);
                System.out.print("Enter " + tempEmployeeName[i] + "'s address: ");
                tempEmployeeAddress[i] = in.nextLine();
                System.out.print("Enter " + tempEmployeeName[i] + "'s salary: ");
                tempEmployeeSalary[i] = in.nextDouble();
                System.out.print("How many bonuses, if any, would you like for " + tempEmployeeName[i] + ": ");
                tempBonus[i] = in.nextInt();
                while(tempBonus[i] >= 10)
                    System.out.println("You have entered more than 10, each employee may only have " +
                    "10 bonues or less. Please re-enter the number of bonuses for " + tempEmployeeName[i] + ": ");
                    tempBonus[i] = in.nextInt();
                if(tempBonus[i] <= 0)
                    System.out.println("You have chosen not to enter any bonuses for " + tempEmployeeName[i] + ".");
                else if (tempBonus[i] > 0)
                    for(int j = 0; j < tempBonus[i]; j++)
                        System.out.print("Enter the amount for bonus " + (j+1) + " for " + tempEmployeeName[i] + ": ");
                        tempEmployeeBonus[i][j] = in.nextDouble();
                for(int l = 0; l < numberOfEmployees; l++)
                    emp[l] = new Employee(tempEmployeeName[l], tempEmployeeNumber[l], tempEmployeeAddress[l], tempEmployeeSalary[l], tempEmployeeBonus[i]);
    The bold line is the only line giving me trouble. Here is the output (italicized text has been typed by ME the user):

    Code (Text):
    You will enter information for some employees, and  then the information will be displayed. We require at least two employees, but you may create more if you wish.
    How many employees would you create: [I]2[/I]
    You will now provide information for 2 employees.
    [B]Enter the employee's name: [/B]'s employee number will be 1001
    Enter 's address: [I]12[/I]
    Enter 's salary: [I]12[/I]
    How many bonuses, if any, would you like for :
    I can enter ALL information properly, but the line that asks for the employee's name is completely skipped. It SHOULD NOT say "'s employee number will be 1001" unless I have typed in a string and then press the return key. I need for the user to press the return key before displaying "'s employee number will be 1001". I thought it might be because I'm using a loop, but that can't be it as I am able to enter the address, salary, etc. perfectly fine.

    2. Relevant equations


    3. The attempt at a solution

    See above code. Your help is much appreciated.
  2. jcsd
  3. May 22, 2012 #2


    User Avatar
    Science Advisor
    Homework Helper

    A call to nextInt() only reads the characters that make up the integer.

    When you type "2 RETURN" to enter the number of employees, nextInt() reads the "2" but not the "RETURN" character. The nextLine() then reads everythng up to the next RETURN, which is nothing. That's why your employee name is blank.

    To see what is happening , type "2 John Doe RETURN" instead of just "2".

    The best way to fix this is to read ALL the input with nextLine(), and then convert the string to an integer. That way, the computer always keeps in step with what the user typed.

    As a quick fix you can add another nextLine() to eat up the RETURN character you don't want.
  4. May 24, 2012 #3
    That makes sense. Program works correctly now. Thank you for your help!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook