Reputation: 884
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
Upvotes: 0
Views: 89
Reputation: 1500
You are splitting on spaces:
String[] inputArray = input.split(" ");
'Bob Jones' contains a space. So your split is likely resulting in elements in different places than you are expecting.
If all file entries will follow a 'two name' with a space format, try the following change:
String department = inputArray[0];
String fullName = inputArray[1] + " " + inputArray[2]
int age = Integer.parseInt(inputArray[3]);
Department d = c.checkDepartment(department);
d.newEmployee(age, fullName, d);
The actual numbers used for the split will obviously depend on the structure of each line. This split assumes the line is in the following format:
Marketing Bob Jones 54
But it would be better practice to wrap each entry with quotes, then use a regular expression to split each 'element' (There are a number of stackoverflow solutions relating to this method). This way both:
Could be processed from the same file, with the same code.
Upvotes: 0
Reputation: 31339
Guessing your input, I'd say this is what you wanted:
String[] inputArray = input.split(" ");
Department d = c.checkDepartment(inputArray[3]);
d.newEmployee(Integer.parseInt(inputArray[2]), inputArray[0] + " " + inputArray[1], d);
Notice that the split
splits on all whitespace, including the first and last name of each employee.
Upvotes: 2