In.nextLine(); does not work properly

  • Thread starter Thread starter stripes
  • Start date Start date
  • Tags Tags
    Work
AI Thread Summary
The issue arises because the use of nextInt() does not consume the newline character, causing the subsequent nextLine() to read an empty string. This leads to the employee's name being skipped in the input process. To resolve this, it's recommended to read all input using nextLine() and then convert the string to an integer for the number of employees. Alternatively, adding an additional nextLine() after nextInt() can effectively consume the leftover newline character. Implementing either solution ensures the program prompts for the employee's name correctly.
stripes
Messages
262
Reaction score
0

Homework Statement



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:
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:
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.

Homework Equations



None

The Attempt at a Solution



See above code. Your help is much appreciated.
 
Physics news on Phys.org
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.
 
That makes sense. Program works correctly now. Thank you for your help!
 

Similar threads

Replies
7
Views
2K
Replies
1
Views
2K
Replies
12
Views
2K
Replies
3
Views
1K
Replies
2
Views
2K
Replies
2
Views
3K
Back
Top