0

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
3
  • 1
    How great would it be if you showed us the code that actually produces the output? Commented Jun 21, 2014 at 1:33
  • give us the input please :) Commented Jun 21, 2014 at 1:39
  • 1
    Note that you can never reliably split names on spaces - There are valid first and last names which contain spaces rather than hyphens such as "Anna Mae van der Westhuizen" for example. Commented Jun 21, 2014 at 1:47

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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:

  • "Marketing" "Bob Jones" "54"
  • "Marketing" "Bob" "54"

Could be processed from the same file, with the same code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.