2017-01-13 26 views
1
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; 
    } 

} 

TextFile劈裂字符串類型,並在循環

什麼是我的代碼的問題放到一個新的陣列?當我運行我的程序時,特定的代碼不會顯示出來。它總是說有問題。當我放置員工代碼時,這總是在控制檯中出現。 員工代碼:A11-0001 異常線程 「main」 java.lang.ArrayIndexOutOfBoundsException:1 在gTextFile.readFile(gTextFile.java:28) 在gTextFile.main(gTextFile.java:17)

+0

開始我想你想'gEmpName(eCode); //變量' –

+0

同時檢查文本文件中的空行 –

+0

拿一個調試器(又名Eclipse),並在發生異常的那一行設置一個斷點。看看'oTemp'和'EmCode'的值,你知道什麼是錯的。 – BetaRide

回答

1

使用oTemp = scnTwo.nextLine();而不是oTemp = scnTwo.next();在您的readFile()方法。在使用next()時,您只是在第一個空格之前得到什麼,在您的情況下只有員工代碼,因此請使用nextLine()來獲取完整的行。欲瞭解更多澄清next()nextLine()之間的差異,請參考以下鏈接

What's the difference between next() and nextLine() methods from Scanner class?

此外,您可能需要使用gEmpName(eCode);代替gEmpName("eCode");

以下是代碼:

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("/home/path/abc.txt"); 
    Scanner scnTwo = new Scanner(nFile); 
    String oTemp; 
     while(scnTwo.hasNext()) { 
     oTemp = scnTwo.nextLine(); 
     String EmCode[] = oTemp.split(" "); 
     String Name[] = EmCode[1].split(","); 
     String idCode = EmCode[0]; 
     String lastname = Name[0]; 
     String firstname = Name[1]; 
     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); 
     } 

    } 

} 

}

+0

我試圖使用nextLine(),但是當我運行它時,它只將代碼放到eCode中,而在控制檯中什麼都沒有顯示出來。當我使用next()時,代碼String Name [] = EmCode [1] .split(「,」)有問題。在readFile()方法中。這個出來總是異常線程 「main」 java.lang.ArrayIndexOutOfBoundsException:1 \t在gTextFile.readFile(gTextFile.java:28) \t在gTextFile.main(gTextFile.java:17) – Sethoy

+0

@sethoy更新的答案,包括代碼。嘗試運行它。 –