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