2016-06-03 5 views
0

텍스트 파일에서 3 줄의 id 블록을 읽는 코드가 있습니다. 그 줄을 String과 int로 나누고 싶습니다. 다른 클래스의 생성자. 지금이 클래스는 ID 번호를 묻고 # 기호 아래의 모든 ID 블록의 세 번째 줄까지를 인쇄합니다. Employee ("Richard Smith", 22, 22)는 id의 객체를 만들기 위해 Employee 클래스의 'name', 'age'및 'job' "전기 기술자"). id에는 파일에 항상 세 개의 행이있을 것입니다. 그래서 어떻게 그 행을 찾아서 String과 int로 나눌 수 있습니까?파일에서 읽은 다음 Java의 생성자에 보내는 방법

#1000 
Richard Smith 
22 
Electric engineer 

#1001 
Elliot Smith 
23 
Physicist 



public class RdB 
{ 
    String b; //file name 

    RdB(String ename) { 
     b = ename; 
    } 

    //info about an employee 
    boolean showE (String id) { 
     int ch; 
     String code, info; 

     //open the file with the info 
     try (BufferedReader showERdr = 
      new BufferedReader (new FileReader(b))) 
     { 
      do { 
       //read characters until a '#' is found 
       ch = showERdr.read(); 

       //check 
       if(ch == '#') { 
        code = showERdr.readLine(); 
        if(id.compareTo(code) == 0) { //found employee 
         do { 
          info = showERdr.readLine(); 
          if(info != null) { 
           System.out.println(info); 
          } 
         } while(((info != null) && 
          (info.compareTo("") != 0))); 
         return true; 
        } 
       } 
      } while(ch != -1); 
     } 
     catch(IOException exc) { 
      System.out.println("File error!"); 
      return false; 
     } 
     return false; //employee not found 
    } 

    //Access a registered employee 
    String getE() { 
     String id = ""; 

     BufferedReader br = new BufferedReader(
       new InputStreamReader(System.in)); 

     System.out.println("Enter id number: "); 
     try{ 
      id = br.readLine(); 
     } 
     catch(IOException exc) { 
      System.out.println("Access error"); 
     } 
     return id; 
    } 
} 

편집 : 당신은 단순히 당신이 파일을 나중에 처리를 읽을 것을 저장하는 ArrayList을 사용할 수 있습니다

class Employee { 
private String name, job; 
private int age; 

    public Employee (String name, int age, String job) { 
     this.name = name; 
     this.age = age; 
     this.job = job; 
    } 

    public String getName() { 
     return name; 
    } 

    public int getAge() { 
     return age; 
    } 

    public String getJob() { 
     return job; 
    } 

} 
+0

방법'Employee' 클래스를 작성하여 시작에 대한 –

+0

@ScaryWombat '클래스 사원 { 개인 문자열 이름, 직업, 개인 int 나이; \t public Employee (문자열 이름, int 나이, 문자열 작업) { \t this.name = name; \t this.age = age; \t this.job = job; } \t public String getName() { \t return name; } \t \t public int getAge() { \t 반환 연령; } \t public String getJob() { \t return job; } \t } ' –

+0

의견을 추가하는 것보다는 질문을 업데이트하는 편이 더 좋습니다. –

답변

1

Employee 클래스. 저장된 파일의 기능에 대해 확신하기 때문에 ID가있는 행 앞에 항상 3 줄이 포함됩니다. 여기에 문제를 해결하기 위해 작성한 작업 코드가 있습니다. 당신은 여기 ()
사이에 포장 출력을 볼 수있는 것은 코드 :

import java.io.*; 
import java.util.List; 
import java.util.ArrayList; 

public class Emp{ 

public static void main(String[] args) throws IOException{ 
    List data = new ArrayList<String>(); 
    BufferedReader reader = new BufferedReader(new FileReader("PeopleData.txt")); 
    String str; 
    while((str = reader.readLine()) != null){ 
//   System.out.println(str); 
     data.add(str); 
    } 
    for(int i= 0; i< data.size(); i ++){ 
       i++; 
       System.out.println("("); 
       System.out.println(data.get(i)); 
       String name = (String) data.get(i); 
       i++; 
       System.out.println(data.get(i)); 
       int age = Integer.valueOf((String) data.get(i)); 
       i++; 
       System.out.println(data.get(i)); 
       String job = (String) data.get(i); 
       i++; 
       System.out.println(")"); 
       //new Employee(name, age, job); 
      } 
} 
} 
+0

변수를 Employee 생성자에 전달하는 위치는 어디입니까? –

+0

끝에있는 주석 @ Rafa_Asp1 –

+0

하지만 Employee.getName(); 아래 (for) 외부에서 "정적이 아닌 메서드 getName() 정적 컨텍스트에서 참조 할 수 없습니다." –

관련 문제