简体   繁体   English

Java I / O中的NoSuchElement异常

[英]NoSuchElement exception in java I/O

I have this method that is supposed to read from a txt file and then print out in the interactions pane the information read. 我有这种方法,应该从txt文件读取,然后在“交互”窗格中打印出读取的信息。 The method works, and I get all the data in the file, but in the end I also get java.util.NoSuchElementException printed out. 该方法有效,并且我获取了文件中的所有数据,但最后我也得到了java.util.NoSuchElementException的打印输出。 I know this means that the scanner has reached the end of the file where there is no more data to read, but do not know how to fix the problem. 我知道这意味着扫描程序已到达文件末尾,那里没有更多数据可读取,但不知道如何解决该问题。 Can someone help me please? 有人能帮助我吗?

public void parseRecord(String recordFileName){

File recordFile = null;
Fruit fruit = null;
String color = null, name = null;
Scanner sc = null;
PrintWriter output = null;      

try{    
  recordFile = new File(recordFileName);
  sc = new Scanner(recordFile);
  sc.useDelimiter("- *");

  while(sc.hasNext()){
    color = sc.next();
    name = sc.next();

    fruit = new Fruit(color, name);
    System.out.println(fruit);
  }
  sc.close();
}catch(Exception e){
  System.out.println(e);
}  

} }


  java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at ReadFruits.parseRecord(ReadFruits.java:21)
at ReadFruits.main(ReadFruits.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.dynamicjava.symbol.JavaClass$JavaMethod.evaluate(JavaClass.java:362)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.handleMethodCall(ExpressionEvaluator.java:92)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.visit(ExpressionEvaluator.java:84)
at koala.dynamicjava.tree.StaticMethodCall.acceptVisitor(StaticMethodCall.java:121)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.value(ExpressionEvaluator.java:38)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.value(ExpressionEvaluator.java:37)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.visit(StatementEvaluator.java:106)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.visit(StatementEvaluator.java:29)
at koala.dynamicjava.tree.ExpressionStatement.acceptVisitor(ExpressionStatement.java:101)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.evaluateSequence(StatementEvaluator.java:66)
at edu.rice.cs.dynamicjava.interpreter.Interpreter.evaluate(Interpreter.java:77)
at edu.rice.cs.dynamicjava.interpreter.Interpreter.interpret(Interpreter.java:47)
at edu.rice.cs.drjava.model.repl.newjvm.InterpreterJVM.interpret(InterpreterJVM.java:246)
at edu.rice.cs.drjava.model.repl.newjvm.InterpreterJVM.interpret(InterpreterJVM.java:220)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

I think the problem might be at the txt file I have created. 我认为问题可能出在我创建的txt文件上。 I use - as a delimiter and in the last line of the file I should not put -, meaning Orange- Orange- Green- Apple 我使用-作为分隔符,并且在文件的最后一行中不应该放置-表示橙色-橙色-绿色-苹果

When I do this I do not get the error anymore 当我这样做时,我不再收到错误

One problem you have is that you aren't checking hasNext for every time you use next : 您遇到的一个问题是,每次使用next时都没有检查hasNext

while(sc.hasNext()) {
    String color = sc.next();
    if(sc.hasNext()) {
        String name = sc.next();
        Fruit fruit = new Fruit(color, name);
        System.out.println(fruit);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM