java.util.NoSuchElementException 在 java 中使用 StringTokenizer
java.util.NoSuchElementException while using StringTokenizer in java
我最近在核心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
我会先在 ListTableClass
中添加一个 toString
,比如
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)
and a Scanner
并从用户的主文件夹中读取文件,最后编程到 List
界面(并使用菱形运算符 <>
) 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 来解析行中的每个元素。但是在这样做的同时,我遇到了以下错误消息。 关于此的任何帮助,在此先感谢。 错误信息:
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
我会先在 ListTableClass
中添加一个 toString
,比如
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)
and a Scanner
并从用户的主文件夹中读取文件,最后编程到 List
界面(并使用菱形运算符 <>
) 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);
}
}