gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:56:53 26 4
gpt4 key购买 nike

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)

最佳答案

readFile() 方法中使用 oTemp = scnTwo.nextLine(); 而不是 oTemp = scnTwo.next();。使用 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);
}

}

}

}

关于java - 拆分字符串类型并放入循环中的新数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41627826/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com