Reputation: 123
For some reason my statement keeps going through to the next line without doing what it is supposed to.
package Employee;
import java.util.Scanner;
import static java.lang.System.out;
public class EmployeeTest {
public static void main (String args[]) {
out.println ("Welcome to employee storer: ");
Scanner imput = new Scanner (System.in);
Employee employee1 = new Employee();// Creates an employee
Employee employee2 = new Employee();
out.println ("Please enter first name: ");
String fName = imput.nextLine(); //reads keyboard
employee1.setfName(fName);// sets first name
out.println("please enter last name: ");
String lName = imput.nextLine();//reads keyboard
employee1.setlName(lName);// sets second name
out.println("Please enter pay: ");
double pay = imput.nextDouble();//reads keyboard
employee1.setPay(pay);//sets the pay
out.println ("Please enter first name: ");
String fName1 = imput.nextLine(); //reads keyboard
employee2.setfName(fName1);// sets first name
out.println("please enter last name: ");
String lName1 = imput.nextLine();//reads keyboard
employee2.setlName(lName1);// sets second name
out.println("Please enter pay: ");
double pay1 = imput.nextDouble();//reads keyboard
employee2.setPay(pay1);//sets the pay
employee1.printer();// prints the info
employee2.printer();// same here
employee1.raise();
}
}
My problem is the second fName imput, it never gets an input from the console, it ends up looking like this
"Welcome to employee storer:
Please enter first name:
Jake
please enter last name:
Smith
Please enter pay:
100
Please enter first name:
please enter last name:
Jake
Please enter pay:
100
The employee Jake Smith yearly salary is: 1200.0
The employee Jake yearly salary is: 1200.0
Your raise is: 120.0
Your new yearly pay is: 1200.0120.0"
My method class looks like this: package Employee; import static java.lang.System.out;
public class Employee {
private String fName;
private String lName;
private double pay;
public void payCheck() {
if (pay > 0 )
pay = 0;
}
public double yearly() {
double overall = pay * 12;
return overall;
}
public void printer() {
out.println( " The employee " + fName + " " + lName +
" yearly salary is: " + pay * 12 );
}
public void raise() {
double year = pay * 12;
double increase = .1;
double raise = increase * year;
out.println("Your raise is: " + raise );
out.print("Your new yearly pay is: " + year + raise);
}
// Getters and setters
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public double getPay() {
return pay;
}
public void setPay(double pay) {
this.pay = pay;
}
}
I also get an odd output for the final yearly pay. Not really sure why, Any help would be appreciated.
Upvotes: 0
Views: 176
Reputation: 194
Also about the last part of your question
out.print("Your new yearly pay is: " + year + raise);
This is doing simple string concatenation, so you get "strange" result. Instead, try an extra pair of brackets for proper summation and then concatenation
out.print("Your new yearly pay is: " + (year + raise));
Upvotes: 1
Reputation: 44740
try this
imput.nextDouble();
just execute below line after nextDouble();
imput.nextLine();
whenever you want to Scan numbers with a .next()
call, make sure you add an empty .nextLine()
next to it so that the Scanner
moves to the correct position
Upvotes: 2
Reputation: 30644
The problem is that you're pressing enter after the double; System.in is getting a double followed by a newline, which gets read as a double (the newline stops the scanning) then tries to read a string up to new line (which results in an empty string)
Upvotes: 2
Reputation: 41958
nextDouble() just reads a double from the input, not a double on an entire line. The carriage return you entered after the double remains in the input, which is immediately retrieved by the nextLine() statement after, not halting.
Retrieve it with nextLine() as for all the others, then convert the result to a double (with errorchecking obviously).
Upvotes: 2
Reputation: 1270
nextDouble()
will return only the Double
value on the line, leaving the line return behind, which is picked up by the next nextLine()
statement.
Upvotes: 5