사용자 입력을 기반으로 직원 이름, 연령 및 부서를 출력하는 기본 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"과 같이 하이픈 대신 공백이 포함 된 성 및 이름이 있습니다. –