我理解没有这样的元素例外,但我不明白我做错了什么。我需要使用 Tokenizer,这样我才能读取诸如“A-902”或“S-823”之类的标记,并识别 0 处的字符以确定员工所在的部门。Information.txt 包含如下条目:
Jane Rivers, A-902, 05/16/2001, 1, 16.25
Bob Cox, S-823, 06/21/1990, 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();
}
}