import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class gTextFile {
static LinkedList<gText> list = new LinkedList<gText>();
public static void main(String[] args) throws FileNotFoundException {
Scanner scnOne = new Scanner(System.in);
String eCode;
System.out.print("Employee Code: ");
eCode = scnOne.nextLine();
readFile();
gEmpName("eCode");
}
public static void readFile() throws FileNotFoundException {
File nFile = new File("C:\\JAVAP\\GetTextFile\\employee.txt");
Scanner scnTwo = new Scanner(nFile);
String oTemp;
while(scnTwo.hasNext()) {
oTemp = scnTwo.next();
String EmCode[] = oTemp.split(" ");
String Name[] = EmCode[1].split(",");
String idCode = EmCode[0];
String lastname = Name[0];
String firstname = Name[1];
//System.out.println("FName " + firstname);
//System.out.println("LName " + lastname);
gText gT = new gText(firstname, lastname, idCode);
list.add(gT);
}
scnTwo.close();
}
public static void gEmpName(String EmpCode) {
Iterator<gText> itr = list.iterator();
while(itr.hasNext()) {
gText gT = itr.next();
if(gT.id.equals(EmpCode)){
System.out.println("Employee Name: " + gT.Fname + " " + gT.Lname);
}
}
}
}
public class gText {
String Fname;
String Lname;
String id;
gText(String First, String Last, String ID) {
this.Fname = First;
this.Lname = Last;
this.id = ID;
}
public String gFName() {
return Fname;
}
public String gLName() {
return Lname;
}
public String gId() {
return id;
}
}
什麼是我的代碼的問題放到一個新的陣列?當我運行我的程序時,特定的代碼不會顯示出來。它總是說有問題。當我放置員工代碼時,這總是在控制檯中出現。 員工代碼:A11-0001 異常線程 「main」 java.lang.ArrayIndexOutOfBoundsException:1 在gTextFile.readFile(gTextFile.java:28) 在gTextFile.main(gTextFile.java:17)
開始我想你想'gEmpName(eCode); //變量' –
同時檢查文本文件中的空行 –
拿一個調試器(又名Eclipse),並在發生異常的那一行設置一個斷點。看看'oTemp'和'EmCode'的值,你知道什麼是錯的。 – BetaRide