텍스트 파일에서 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;
}
}
방법'Employee' 클래스를 작성하여 시작에 대한 –
@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 } ' –
의견을 추가하는 것보다는 질문을 업데이트하는 편이 더 좋습니다. –