• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Java Scanner .newLine Question

00PS

[H]ard|Gawd
Joined
May 5, 2009
Messages
1,798
I'm having an issue with Java and the .newLine method. I have a very simple program that asks for employee information (first/last name and monthly salary) for 2 employee objects. When I prompt for the first employee using:

Code:
// **Employee1 Inputs**
		System.out.print("Please enter Employee1's first name: "); // prompt
		firstNameValue = input.nextLine(); // record user string input
		employee1.setFirstName(firstNameValue); // sends values to     setFirstName method of Employee class

		System.out.print("Please enter Employee1's last name: "); // prompt
		lastNameValue = input.nextLine(); // record user string input
		employee1.setLastName(lastNameValue); // sends values to setLastName method of Employee class

		System.out.print("Please enter Employee1's monthly salary: "); // prompt
		salaryValue = input.nextDouble(); // record user decimal input
		employee1.setMonthlySalary(salaryValue); // sends values to setMonthlySalary method of Employee class

Code:
When run, it looks like this:
Please enter Employee1's first name: john
Please enter Employee1's last name: doe
Please enter Employee1's monthly salary: 400.00

The code runs and spaces every prompt to a new line. I liked having user input on the same line that the prompt was on hence only using System.out.print. So here is where it gets weird, employee2's info is now prompted using the same code minus the replacement of the employee2 object name. Here is the code:
Code:
// **Employee2 Inputs**
		System.out.print("Please enter Employee2's first name: "); // prompt
		firstNameValue = input.nextLine(); // record user string input ---- *PROBLEM WITH .NEXTLINE
		employee2.setFirstName(firstNameValue); // assign first name values for employee2 using the setFirstName method

		System.out.print("Please enter Employee2's last name: "); // prompt
		lastNameValue = input.nextLine(); // record user string input ---- *PROBLEM WITH .NEXTLINE
		employee2.setLastName(lastNameValue); // assign first name values for employee2 using the setFirstName method

		System.out.print("Please enter Employee2's monthly salary: "); // prompt
		salaryValue = input.nextDouble(); // record user decimal input
		employee2.setMonthlySalary(salaryValue); // assign salary values for employee2 using the setMonthlySalary method

Code:
When run, it looks like this:
Please enter Employee2's first name: Please enter Employee2's last name: sadman
Please enter Employee2's monthly salary: 500.00

Using the same .nextLine both prompts for First Name and Last Name run together and stop at the last name. The input will only take/store the last name. So beside using the .next instead of .nextLine is there any way to stop those two System.out.print statements from running together? If it worked for employee1 I see no reason why employee2 is any different.

Keep in mind, this is a first semester java programming course so for you expert programmers, try to keep it relatively simplistic. I guess I'm looking for a pause method or something...

Thanks guys. If need be, I can give the entire code for both classes.
 
Last edited:
Without seeing more code I don't know exactly why it's prompting for Employee1 again, but I do know what is causing it to skip the input for the first name the second time.Whenever you use a Scanner to get a numerical type it will leave the newline character on the buffer. Try adding an input.nextLine() after calling salary=input.nextDouble() to see if that fixes it.
 
oops, sorry that was a mistake when I copied the output of the second one, it should read:
Code:
When run, it looks like this:
Please enter Employee2's first name: Please enter Employee1's last name: sadman
Please enter Employee2's monthly salary: 500.00

employee1 isn't being input again. Sorry for the poor communication on my part.

In otherwords, the employee2 prompts are two fast and not giving the user a chance to input the First Name before moving to the next prompt for the Last Name.
 
Last edited:
So with more Googling I think I've found the problem. Seems there is an apparent issue with Java when nextLine picks up a string after a integer/decimal. Here is a blog that I found on the issue: http://blog.ryanrampersad.com/2008/12/19/stop-input-skipping-in-java/

So I added a blank
Code:
input.nextLine();

after both salary inputs and it works as expected now.

Code:
// **Employee1 Inputs**
		System.out.print("Please enter Employee1's first name: "); // prompt
		firstNameValue = input.nextLine(); // record user string input
		employee1.setFirstName(firstNameValue); // sends values to setFirstName method of Employee class

		System.out.print("Please enter Employee1's last name: "); // prompt
		lastNameValue = input.nextLine(); // record user string input
		employee1.setLastName(lastNameValue); // sends values to setLastName method of Employee class

		System.out.print("Please enter Employee1's monthly salary: "); // prompt
		salaryValue = input.nextDouble(); // record user decimal input
		input.nextLine();
		employee1.setMonthlySalary(salaryValue); // sends values to setMonthlySalary method of Employee class

// **Employee2 Inputs**
		System.out.print("Please enter Employee2's first name: "); // prompt
		firstNameValue = input.nextLine(); // record user string input ---- *PROBLEM WITH .NEXT
		employee2.setFirstName(firstNameValue); // assign first name values for employee2 using the setFirstName method

		System.out.print("Please enter Employee2's last name: "); // prompt
		lastNameValue = input.nextLine(); // record user string input ---- *PROBLEM WITH .NEXT
		employee2.setLastName(lastNameValue); // assign first name values for employee2 using the setFirstName method

		System.out.print("Please enter Employee2's monthly salary: "); // prompt
		salaryValue = input.nextDouble(); // record user decimal input
		input.nextLine();
		employee2.setMonthlySalary(salaryValue); // assign salary values for employee2 using the setMonthlySalary method
 
Back
Top