java.util.NoSuchElementException : No line found*

标签 java nosuchelementexception

我不断收到此错误 java.util.NoSuchElementException 找不到行 当我使用这个方法时

public boolean hasMoreCommands() {
    if (input.hasNextLine()) {
        return true;
    } else {
        //input.close();
        return false;
    }
}

public void advance() {
    String str;
    if(hasMoreCommands() == true){
        do {
            str = input.nextLine().trim();

            // Strip out any comments
            if (str.contains("//")) {
                str = (str.substring(0, str.indexOf("//"))).trim();
            }
        } while (str.startsWith("//") || str.isEmpty() || hasMoreCommands());
        command = str;
  }
}

我这里有主要代码:

public class Ptest
{
  public Ptest(String fileName)
  {
    String line = null;
    String nName = fileName.replace(".vm", ".asm");
    Parser p = new Parser();

    try{ 
        File neF = new File(nName);
        if(!neF.exists()){
            neF.createNewFile();
        }
        File tempFile = new File("temp.txt");
        if(!tempFile.exists()){
            tempFile.createNewFile();
        }

        FileReader fr = new FileReader(fileName);
        BufferedReader br = new BufferedReader(fr);
        FileWriter fw = new FileWriter(nName);
        BufferedWriter bw = new BufferedWriter(fw);
        FileWriter writR = new FileWriter(tempFile);
        BufferedWriter buffR = new BufferedWriter(writR);
        while((line = br.readLine()) != null) {
            buffR.write(line+ "\n");
            //System.out.println(line);
        }  
        buffR.flush();
        buffR.close();
        p.insertTitle(tempFile);
        String ctype = p.commandType();
        int len = ctype.length();
        int spaces = 13 - len;
        String sp = " ";
        String asp = " ";
        String a1 = null;
        int a2;
        int alen;
        boolean t = false;
        while(p.hasMoreCommands()){
            for(int i= 0; i < spaces; i++){
                sp += " ";
            }
            t = p.hasMoreCommands();
            a1 = p.arg1();
            alen = (10 - a1.length());
            for(int i= 0; i < alen; i++){
                asp += " ";
            }
            //a2 = p.arg2();
            if (ctype == "C_PUSH" || ctype == "C_POP" || ctype == "C_FUNCTION" || ctype == "C_CALL") {
                a2 = p.arg2(); 
                bw.write(ctype + sp + a1 + asp + a2);
            }
            else {
                bw.write(ctype + sp + a1);
            }
            p.advance();
            ctype = p.commandType();
            len = ctype.length();
            spaces = 13 - len;
        }

        bw.flush();
        bw.close();
    }
    catch(FileNotFoundException ex){
        System.out.println("File not found!");
    }
    catch(IOException ex){
        System.out.println("Error reading file '"  + fileName + "'");
    }
  }
}

我通过了调试器,它实际上运行了整个文件,然后在完成时给我一个错误。

最佳答案

像@hfontanez一样,我认为你的问题出在这段代码中:

if(hasMoreCommands() == true){
    do {
        str = input.nextLine().trim();

        // Strip out any comments
        if (str.contains("//")) {
            str = (str.substring(0, str.indexOf("//"))).trim();
        }
    } while (str.startsWith("//") || str.isEmpty() || hasMoreCommands());
    command = str;
}

但是,我的解决方案是将 while 子句更改为 while (str.isEmpty() && hasMoreCommands());

我假设“advance”应该返回下一个非注释/空行。

如果前一遍的字符串为空(在删除任何注释后),它将再次循环,前提是这不是最后一行。但是,如果这是最后一行或者 str 中仍有内容,那么它将退出循环。评论应该被删除,所以暂时不需要测试。

我认为,如果您只是在循环内测试 hasNextLine,那么如果最后一行是注释/空白,它将永远不会退出循环。

关于java.util.NoSuchElementException : No line found*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26708184/

相关文章:

Java Singleton bean 创建列表的多个实例

java - 如何在 Spring MVC 中的同一 @RequestMapping 中为参数映射不同的值?

Java Realm 超出范围异常

java - 扫描仪 - java.util.NoSuchElementException

java - 在进行简单的文件处理时如何修复 NoSuchElementException?

java - 为什么我收到 NoSuchElementException?

java - 如何从 Java 调用 Flex/Flash/Actionscript 方法?

java - 用户定义的数据类型遇到错误的列类型

java - 重新扫描仪的.next()方法: What happens after last valid token read?

java - 线程 "main"java.lang.NoSuchMethodException : after I add ArrayList 中的异常