我在寫一個基本的Java程序,根據用戶輸入輸出員工姓名,年齡和部門。它是有效的,除了輸出是在員工的名字和其他信息和部門沒有顯示之後放在輸出中。我懷疑它與我正在使用的空格分隔符有關,但我不知道爲什麼。任何幫助都是極好的。Java輸出循環錯誤
代碼:
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();
}
}
}
}
預期輸出:
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
實際輸出:
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
如果您向我們展示實際產生輸出的代碼,它會有多好? –
給我們輸入請:) –
請注意,你永遠不能可靠地拆分空間名稱 - 有效的名字和姓氏包含空格而不是連字符,例如「Anna Mae van der Westhuizen」。 –