gpt4 book ai didi

在 java 中使用 StringTokenizer 时出现 java.util.NoSuchElementException

转载 作者:行者123 更新时间:2023-11-30 08:08:57 25 4
gpt4 key购买 nike

我最近正在做一个关于核心java中的文件读取、写入和比较字符串的简单项目。我使用 StringTokenizer 来解析行中的每个元素。但在这样做时,我遇到以下错误消息。对此有任何帮助,请提前致谢。错误信息:

Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(Unknown Source)
at java.util.StringTokenizer.nextElement(Unknown Source)
at com.dhiraj.demo.ComparatorClass.main(ComparatorClass.java:64)

我的代码如下;

String listtable = "listtable.dat";
ArrayList<ListTableClass> ltc = new ArrayList<ListTableClass>();

try(FileInputStream fis = new FileInputStream(listtable);
BufferedReader bio = new BufferedReader(new InputStreamReader(fis))){
String line;
while((line = bio.readLine())!=null){
System.out.println(line);
StringTokenizer to = new StringTokenizer(line, "|");
//while(tokens.hasMoreElements()){
//System.out.println(tokens.nextElement());

//Integer id = Integer.parseInt(to.nextElement().toString().trim());
// System.out.println(id);
Integer id = Integer.parseInt(to.nextElement().toString());

//int id = Integer.parseInt(to.nextElement().toString().trim());
System.out.println("i am error");
String fname = to.nextElement().toString().trim();
String lname = to.nextElement().toString().trim();
String address = to.nextElement().toString().trim();
Integer age = Integer.parseInt(to.nextElement().toString().trim());


ltc.add(new ListTableClass(id, fname, lname, address, age));

//}
}
}catch(IOException e){
System.out.println(e);

}
for(ListTableClass t: ltc){
System.out.println(t.getId());
System.out.println(t.getFname());
System.out.println(t.getLname());
System.out.println(t.getAddress());
System.out.println(t.getAge());

}

文件示例是:

11120|bijaya|khanal|biratnagar|25
11121|chandramani|sapkota|hetauda|25
11117|Hari|Sapkota|bhaktapur|25
11118|pramod|chaulagain|banepa|25
11119|bigyan|shrestha|birjung|25
11120|bijaya|khanal|biratnagar|25
11114|suman|hamal|shangrila|25
11115|kishor|Neupane|Sanepa|25

最佳答案

我首先添加 toStringListTableClass ,类似

public String toString() {
return String.format("Id: %d%n" + //
"Name (lname, fname): %s, %s%n" + //
"Address: %s%n" + //
"Age: %d",//
id, lname, fname, address, age//
);
}

那我更喜欢 String.split(String) 和一个 Scanner 并从用户的主文件夹中读取文件,最后编程到List<> 的接口(interface)(并使用菱形运算符 toString )你可能应该检查空行,例如

public static void main(String[] args) {
String listtable = "listtable.dat";
List<ListTableClass> ltc = new ArrayList<>();
File f = new File(System.getProperty("user.home"), listtable);
try (Scanner s = new Scanner(f)) {
while (s.hasNextLine()) {
String line = s.nextLine();
if (line.trim().isEmpty()) {
continue;
}
System.out.println(line);
String[] to = line.split("|");
Integer id = Integer.parseInt(to[0].trim());
String fname = to[1].trim();
String lname = to[2].trim();
String address = to[3].trim();
Integer age = Integer.parseInt(to[4].trim());
ltc.add(new ListTableClass(id, fname, lname, address, age));
}
} catch (IOException e) {
System.out.println("Caught Exception: " + e.getMessage());
e.printStackTrace();
}
for (ListTableClass t : ltc) {
System.out.println(t);
}
}

关于在 java 中使用 StringTokenizer 时出现 java.util.NoSuchElementException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30689535/

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