In.nextLine(); does not work properly

  • Thread starter Thread starter stripes
  • Start date Start date
  • Tags Tags
    Work
Click For Summary
SUMMARY

The issue discussed revolves around the Java program's inability to properly capture user input for employee names due to the use of nextInt() followed by nextLine(). When nextInt() is called, it consumes only the integer input and leaves the newline character in the input buffer, causing the subsequent nextLine() to read an empty string. The solution is to either read all input using nextLine() and convert it to an integer or to add an additional nextLine() call after nextInt() to clear the buffer.

PREREQUISITES
  • Understanding of Java input/output methods, specifically nextInt() and nextLine()
  • Basic knowledge of Java arrays for storing employee information
  • Familiarity with control flow in Java, including loops and conditionals
  • Experience with debugging Java programs to identify input-related issues
NEXT STEPS
  • Learn how to effectively use Scanner class methods in Java for user input
  • Research Java exception handling to manage input errors gracefully
  • Explore Java array manipulation techniques for dynamic employee data storage
  • Investigate best practices for user input validation in Java applications
USEFUL FOR

Java developers, programming students, and anyone involved in creating interactive console applications that require user input handling.

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 18 ·
Replies
18
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 37 ·
2
Replies
37
Views
5K