2014-06-21 5 views
0

사용자 입력을 기반으로 직원 이름, 연령 및 부서를 출력하는 기본 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 
+1

출력을 실제로 생성하는 코드를 보여 주면 얼마나 좋을까요? –

+0

입력 해주십시오 :) –

+1

공백에 이름을 안정적으로 분할 할 수는 없습니다. 예를 들어 "Anna Mae van der Westhuizen"과 같이 하이픈 대신 공백이 포함 된 성 및 이름이 있습니다. –

답변

2

입력을 추측, 나는 이것이 당신이 원하는 무엇을 말하고 싶지만 :

String[] inputArray = input.split(" "); 
Department d = c.checkDepartment(inputArray[3]); 
d.newEmployee(Integer.parseInt(inputArray[2]), inputArray[0] + " " + inputArray[1], d); 

주의 각 직원의 이름과 성을 포함한 모든 공백에 split 분할.

0

당신은 공간에 분할됩니다

String[] inputArray = input.split(" "); 

'Bob Jones'에 공백이 있습니다. 그래서 당신의 분할은 당신이 기대하는 것보다 다른 장소에있는 요소를 초래할 가능성이 큽니다. 각 라인의 구조에 따라 분명합니다 분할에 사용

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); 

실제 번호 : 모든 파일 항목에 공백 형식으로 '두 개의 이름을'따를 경우

는 다음과 같이 변경하십시오.

마케팅 밥 존스 (54)

하지만, 각 '요소'를 분할하는 정규 표현식을 사용 따옴표 각 항목을 포장하는 좋은 연습이 될 것이다 (There are a number of stackoverflow solutions relating to this method을)이 분할 선이 다음과 같은 형식으로 가정합니다 . 이 방법 모두 :

  • "마케팅" "밥 존스", "54"
  • "마케팅" "밥", "54"

는 동일한 코드와 같은 파일에서 처리 될 수 있습니다 .

관련 문제