Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
拆分字符串类型并放入循环中的新数组 导入java.io.File; 导入java.io.FileNotFoundException; 导入java.util.*; 公共类gTextFile{ 静态LinkedList=新建LinkedList(); 公共静态void main(字符串[]args)引发FileNotFoundException{ Scanner scnOne=新扫描仪(System.in); 字符串编码; 系统输出打印(“员工代码:”); eCode=scnOne.nextLine(); readFile(); gEmpName(“eCode”); } public static void readFile()引发FileNotFoundException{ File nFile=新文件(“C:\\JAVAP\\GetTextFile\\employee.txt”); 扫描仪scnTwo=新扫描仪(nFile); 字符串oTemp; while(scnTwo.hasNext()){ oTemp=scnTwo.next(); 字符串EmCode[]=oTemp.split(“”); 字符串名称[]=EmCode[1]。拆分(“,”; 字符串idCode=EmCode[0]; 字符串lastname=Name[0]; 字符串firstname=Name[1]; //System.out.println(“FName”+firstname); //System.out.println(“LName”+lastname); gText gT=新的gText(名字、姓氏、识别码); 列表。添加(gT); } scnTwo.close(); } 公共静态void gEmpName(字符串EmpCode){ 迭代器itr=list.Iterator(); while(itr.hasNext()){ gText gT=itr.next(); 如果(燃气轮机id等于(EmpCode)){ System.out.println(“员工姓名:+gT.Fname+”+gT.Lname); } } } } 公共类gText{ 字符串Fname; 字符串名称; 字符串id; gText(字符串第一,字符串最后,字符串ID){ this.Fname=First; this.Lname=Last; this.id=id; } 公共字符串gFName(){ 返回Fname; } 公共字符串gLName(){ 返回Lname; } 公共字符串gId(){ 返回id; } }_Java_File_Io_Readfile - Fatal编程技术网

拆分字符串类型并放入循环中的新数组 导入java.io.File; 导入java.io.FileNotFoundException; 导入java.util.*; 公共类gTextFile{ 静态LinkedList=新建LinkedList(); 公共静态void main(字符串[]args)引发FileNotFoundException{ Scanner scnOne=新扫描仪(System.in); 字符串编码; 系统输出打印(“员工代码:”); eCode=scnOne.nextLine(); readFile(); gEmpName(“eCode”); } public static void readFile()引发FileNotFoundException{ File nFile=新文件(“C:\\JAVAP\\GetTextFile\\employee.txt”); 扫描仪scnTwo=新扫描仪(nFile); 字符串oTemp; while(scnTwo.hasNext()){ oTemp=scnTwo.next(); 字符串EmCode[]=oTemp.split(“”); 字符串名称[]=EmCode[1]。拆分(“,”; 字符串idCode=EmCode[0]; 字符串lastname=Name[0]; 字符串firstname=Name[1]; //System.out.println(“FName”+firstname); //System.out.println(“LName”+lastname); gText gT=新的gText(名字、姓氏、识别码); 列表。添加(gT); } scnTwo.close(); } 公共静态void gEmpName(字符串EmpCode){ 迭代器itr=list.Iterator(); while(itr.hasNext()){ gText gT=itr.next(); 如果(燃气轮机id等于(EmpCode)){ System.out.println(“员工姓名:+gT.Fname+”+gT.Lname); } } } } 公共类gText{ 字符串Fname; 字符串名称; 字符串id; gText(字符串第一,字符串最后,字符串ID){ this.Fname=First; this.Lname=Last; this.id=id; } 公共字符串gFName(){ 返回Fname; } 公共字符串gLName(){ 返回Lname; } 公共字符串gId(){ 返回id; } }

拆分字符串类型并放入循环中的新数组 导入java.io.File; 导入java.io.FileNotFoundException; 导入java.util.*; 公共类gTextFile{ 静态LinkedList=新建LinkedList(); 公共静态void main(字符串[]args)引发FileNotFoundException{ Scanner scnOne=新扫描仪(System.in); 字符串编码; 系统输出打印(“员工代码:”); eCode=scnOne.nextLine(); readFile(); gEmpName(“eCode”); } public static void readFile()引发FileNotFoundException{ File nFile=新文件(“C:\\JAVAP\\GetTextFile\\employee.txt”); 扫描仪scnTwo=新扫描仪(nFile); 字符串oTemp; while(scnTwo.hasNext()){ oTemp=scnTwo.next(); 字符串EmCode[]=oTemp.split(“”); 字符串名称[]=EmCode[1]。拆分(“,”; 字符串idCode=EmCode[0]; 字符串lastname=Name[0]; 字符串firstname=Name[1]; //System.out.println(“FName”+firstname); //System.out.println(“LName”+lastname); gText gT=新的gText(名字、姓氏、识别码); 列表。添加(gT); } scnTwo.close(); } 公共静态void gEmpName(字符串EmpCode){ 迭代器itr=list.Iterator(); while(itr.hasNext()){ gText gT=itr.next(); 如果(燃气轮机id等于(EmpCode)){ System.out.println(“员工姓名:+gT.Fname+”+gT.Lname); } } } } 公共类gText{ 字符串Fname; 字符串名称; 字符串id; gText(字符串第一,字符串最后,字符串ID){ this.Fname=First; this.Lname=Last; this.id=id; } 公共字符串gFName(){ 返回Fname; } 公共字符串gLName(){ 返回Lname; } 公共字符串gId(){ 返回id; } },java,file,io,readfile,Java,File,Io,Readfile,我的代码中有什么问题??当我运行程序时,特定的代码不会显示出来。它总是说有问题。当我输入员工代码时,这总是出现在控制台中。 员工代码:A11-0001 线程“main”java.lang.ArrayIndexOutOfBoundsException中出现异常异常:1 在gTextFile.readFile(gTextFile.java:28) 在gTextFile.main(gTextFile.java:17)中使用oTemp=scnTwo.nextLine()而不是oTemp=scnTwo.n

我的代码中有什么问题??当我运行程序时,特定的代码不会显示出来。它总是说有问题。当我输入员工代码时,这总是出现在控制台中。 员工代码:A11-0001 线程“main”java.lang.ArrayIndexOutOfBoundsException中出现异常异常:1 在gTextFile.readFile(gTextFile.java:28)
在gTextFile.main(gTextFile.java:17)中使用
oTemp=scnTwo.nextLine()
而不是
oTemp=scnTwo.next()readFile()方法中的code>。当使用
next()
时,您只获取第一个空格之前的内容,在您的情况下,仅获取员工代码,因此使用
nextLine()
获取完整的行。有关
next()
nextLine()
之间区别的更多说明,请参阅下面的链接

此外,您可能需要使用
gEmpName(eCode)而不是
gEmpName(“eCode”)

代码如下:

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

}
公共类gTextFile{
静态LinkedList=新建LinkedList();
公共静态void main(字符串[]args)引发FileNotFoundException{
Scanner scnOne=新扫描仪(System.in);
字符串编码;
系统输出打印(“员工代码:”);
eCode=scnOne.nextLine();
readFile();
gEmpName(eCode);
}
public static void readFile()引发FileNotFoundException{
File nFile=新文件(“/home/path/abc.txt”);
扫描仪scnTwo=新扫描仪(nFile);
字符串oTemp;
while(scnTwo.hasNext()){
oTemp=scnTwo.nextLine();
字符串EmCode[]=oTemp.split(“”);
字符串名称[]=EmCode[1]。拆分(“,”;
字符串idCode=EmCode[0];
字符串lastname=Name[0];
字符串firstname=Name[1];
gText gT=新的gText(名字、姓氏、识别码);
列表。添加(gT);
}
scnTwo.close();
}
公共静态void gEmpName(字符串EmpCode){
迭代器itr=list.Iterator();
while(itr.hasNext()){
gText gT=itr.next();
如果(燃气轮机id等于(EmpCode)){
System.out.println(“员工姓名:+gT.Fname+”+gT.Lname);
}
}
}

}

对于start,我想您需要
gEmpName(eCode);//变量
还检查文本文件中是否有空行。使用调试器(也称为Eclipse)并在获取异常的行上设置断点。查看
oTemp
EmCode
的值,您就知道问题出在哪里了。我尝试使用nextLine(),但当我运行它时,它只将代码放入eCode,而控制台中没有显示任何内容。当我使用next()时,代码字符串名称[]=EmCode[1]出现问题;在readFile()方法中。这总是会出现,线程“main”java.lang.ArrayIndexOutOfBoundsException:1在gTextFile.readFile(gTextFile.java:28)在gTextFile.main(gTextFile.java:17)@sethoy更新了答案并包含了代码。试着运行它。
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);
        }

    }

}