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

时间:2017-01-13 05:20:03

标签: java file io readfile

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         at gTextFile.readFile(gTextFile.java:28)         在gTextFile.main(gTextFile.java:17)

1 个答案:

答案 0 :(得分: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);
        }

    }

}

}