拆分字符串类型并放入循环中的新数组

Spliting A String Type and put Into a new array in a Loop

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)

readFile() 方法中使用 oTemp = scnTwo.nextLine(); 而不是 oTemp = scnTwo.next();。当使用 next() 时,你只是得到第一个 space 之前的内容,在你的情况下只有员工代码,所以使用 nextLine() 来获取完整的行。有关 next()nextLine() 之间差异的更多说明,请参阅下文 link

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);
        }

    }

}

}