해당 요소 예외가 없다는 것을 이해하지만 잘못된 것을 이해하지 못합니다. "A-902"또는 "S-823"과 같은 토큰을 읽을 수 있도록 Tokenizer를 사용해야하고 직원이 어느 부서에 있는지 확인하기 위해 문자 0을 식별합니다. Information.txt에는해당 요소 예외 없음
과 같은 항목이 포함되어 있습니다.제인 강, A-902, 2001년 5월 16일, 1, 16.25
당신은 너무 많은 token.nextToken()
를 호출 밥 콕스, S-823, 1990년 6월 21일, 2, 17.50
import java.util.Scanner;
import java.io.*;
import java.util.StringTokenizer;
public class CreateFile {
public static void main(String[] args)throws FileNotFoundException{
File newFile = new File("Information.txt");
Scanner readFile = new Scanner(newFile);
PrintWriter outFile = new PrintWriter("Department.txt");
String[] employees = new String[9];
while(readFile.hasNext()){
for(int i=0; i<employees.length; i++){
employees[i] = readFile.nextLine();
}
}
for(int k=0; k<employees.length; k++){
StringTokenizer token = new StringTokenizer(employees[k],",");
while(token.hasMoreTokens()){
outFile.print(token.nextToken());
if(token.nextToken().charAt(0)=='A'){
outFile.print(token.nextToken());
outFile.print("Accounting ");
}else{
if(token.nextToken().charAt(0)=='H'){
outFile.print(token.nextToken());
outFile.print("Human Resources ");
}else{
if(token.nextToken().charAt(0)=='P'){
outFile.print(token.nextToken());
outFile.print("Production ");
}else{
if(token.nextToken().charAt(0)=='S'){
}
outFile.print(token.nextToken());
outFile.print("Shipping");
}
}
}
}
}
readFile.close();
outFile.close();
}
}
'token.nextToken()'을 호출 할 때마다 다음 토큰을 리턴하고 다시 호출 할 준비가 된 다음 토큰으로 진행합니다. 반복마다 한 번만 호출하려고합니다. –
맞습니다. –