i cannot figure out what's wrong
I think i am doing it right but the program skips line 30 where it asks user to insert first name. i dont know why this happens and would like help.
code is unfinish btw i just needed to fix this before i want to move forward
Code JAVA:
import java.util.Scanner;
public class EmployeeTest {
public static void main(String[] args){
String yourName;
String yourLastName;
double yourSalary;
Scanner input = new Scanner(System.in);
Employee applicant1 = new Employee("Trung","Lam",2729.00);
Employee applicant2 = new Employee("Vanessajoie","Castillo",4055.00);
System.out.printf("Initial employee1 is %s %s and makes $%.2f monthly\n",
applicant1.getName(), applicant1.getSurName(), applicant1.getSalary());
System.out.printf("Initial employee2 is %s %s and makes $%.2f monthly\n",
applicant2.getName(), applicant2.getSurName(), applicant2.getSalary());
//set up new employee1
System.out.print("New employee1 first name: ");
yourName = input.nextLine();
System.out.print("New employee1 last name: ");
yourLastName = input.nextLine();
System.out.print("New employee1 salary: $");
yourSalary = input.nextDouble();
applicant1.setName(yourName);
applicant1.setSurName(yourLastName);
applicant1.setSalary(yourSalary);
//set up new employee2
System.out.print("New employee2 first name: ");
yourName = input.nextLine();
System.out.print("New employee2 last name: ");
yourLastName = input.nextLine();
System.out.print("New employee2 salary: $");
yourSalary = input.nextDouble();
applicant2.setName(yourName);
applicant2.setSurName(yourLastName);
applicant2.setSalary(yourSalary);
//list current employees
System.out.printf("Current employee1 is %s %s and makes $%.2f monthly\n",
applicant1.getName(), applicant1.getSurName(), applicant1.getSalary());
System.out.printf("Current employee2 is %s %s and makes $%.2f monthly\n",
applicant2.getName(), applicant2.getSurName(), applicant2.getSalary());
}
}
Code JAVA:
public class Employee {
private String name;
private String surName;
private double salary;
public Employee(String eName, String eSurName, double eSalary){
name = eName;
surName = eSurName;
salary = eSalary;
}
//setting values
public void setName(String eName){
name = eName;
}
public void setSurName(String eSurName){
surName = eSurName;
}
public void setSalary(double eSalary){
if (eSalary >= 0)
salary = eSalary;
else
salary = 0.0;
}
//get values
public String getName(){
return name;
}
public String getSurName(){
return surName;
}
public double getSalary(){
return salary;
}
}
Re: i cannot figure out what's wrong
A pitfall of using nextInt and nextDouble is that those methods do not consume the end of line. When user enters the salary of employee 1 the EOL is still in the buffer. When the program reaches the line asking for the name of employee 2 it scans the EOL still in the buffer and returns an empty String. Simple solution: add a call to nextLine after the call to nextDouble.
Re: i cannot figure out what's wrong
Thanks it worked! but what does EOL stand for?
Re: i cannot figure out what's wrong
Read my post again. It is short for something I mentioned earlier.