I am writing a basic java program to output employee names, ages and departments based on user input. It works, except, the output is taking the employee's first name and placing it in the output after the other information and the department is not displayed. I suspect it has something to do with the space delimiter I am using, but I am not sure why. Any help would be awesome.
Code:
package SimpleJavaAssignment;
import java.util.*;
public class Company
{
ArrayList<Department> deptList = new ArrayList<Department>();
public Department checkDepartment(String name)
{
for(Department dept: deptList)
{
if(dept.getName().equals(name))
{
return dept;
}
}
Department d = new Department(name);
deptList.add(d);
return d;
}
public static void main(String[] args)
{
System.out.println ("Please enter the employee information. First Name, Last Name, Age and Department, and press enter.");
System.out.println ("Once complete entering employee information, press enter a second time.");
Scanner in = new Scanner(System.in);
Company c = new Company();
String input = in.nextLine();
while(in.hasNextLine() && input.length() != 0)
{
String[] inputArray = input.split(" ");
Department d = c.checkDepartment(inputArray[0]);
d.newEmployee(Integer.parseInt(inputArray[2]), inputArray[1], d);
input = in.nextLine();
}
for(Department dept:c.deptList)
{
ArrayList<Employee> empList = dept.getEmployees();
for(Employee emp: empList)
{
emp.printInfo();
}
}
}
}
Expected output:
Employee Name: Bob Jones Employee Age: 38 Department: Marketing Age Is A Prime: false
Employee Name: Alonzo Morris Employee Age: 54 Department: Accounting Age Is A Prime: false
Employee Name: Beth Moore Employee Age: 27 Department: Tech Age Is A Prime: false
Actual output:
Employee Name: Jones Employee Age: 38 Department: Bob Age Is A Prime: false
Employee Name: Morris Employee Age: 54 Department: Alonzo Age Is A Prime: false
Employee Name: Moore Employee Age: 27 Department: Beth Age Is A Prime: false