The goal of my program is to store a default of 100 Employee objects(but should work with less) to an array after prompting the user input of each employee's name, ssn, and salary with a loop.I then need to output the contents of the array. I am trying to end the loop when the user inputs a 0 when asked for a name, but so far my program seems to loop endlessly.
Driver class:
import java.util.Scanner;
public class EmployeeDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner kb = new Scanner(System.in);
Employee employees[]= new Employee[100];
String nameTest="";
for(int i=0;i<100;i++){
Employee e = new Employee();
System.out.println("Enter Employee name, Ssn, and salary when prompted");
System.out.println("When finished type in 0's for each prompt");
System.out.println("Enter Name: ");
nameTest=kb.next();
e.setName(nameTest);
System.out.println("Enter Ssn: ");
e.setSsn(kb.next());
System.out.println("Enter Salary: ");
e.setSalary(kb.nextDouble());
if(nameTest=="0"){
i=100;
}
else
employees[i]=e;
}
for(int x=0;x<100;x++){
employees[x].toString();
}
}
}
Methods:
public class Employee extends Person {
String ssn;
double salary;
public String getSsn() {
return ssn;
}
public void setSsn(String ssn) {
this.ssn = ssn;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Employee() {
super();
// TODO Auto-generated constructor stub
}
public Employee(String initialName,String initialSsn,double initialSalary) {
super(initialName);
ssn=initialSsn;
salary=initialSalary;
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Name: "+getName()+" Ssn: "+getSsn()+" Salary: "+getSalary();
}
}
equalsto compare strings.