2014-11-03 47 views
1

我不断收到此错误 java.util.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 + "'"); 
    } 
    } 
} 

我通过调试程序,它真的去了整个文件,然后完成后给我一个错误。

+0

请在您的帖子中包含整个异常消息。 – furkle 2014-11-03 05:20:35

回答

1

像@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());

我假设“提前”应该返回下一个非评论/空白行。

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

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

+0

非常感谢你的魅力! – 2014-11-03 17:58:22

+0

很好用!我看到你在这里是新的 - 当你得到满足你需要的答案时,所做的就是接受答案。 – Dan 2014-11-04 12:13:21

0

我的猜测是,你的问题就在这里:

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; 
} 

你遇到的异常(NoSuchElementException)当有人试图尽管迭代东西(字符串标记,地图等),而无需首先,如果检查通常发生在有有更多的元素可以获得。第一次执行上面的代码时,它会检查它是否有更多的命令,然后进入循环。然而,如果第一次它应该正常工作,如果while()完成的测试成功,那么当它尝试执行input.nextLine()时,下一次迭代将会爆炸。在调用此方法之前,您必须检查是否有下一行。围绕这条线if(input.hasNextLine()),我认为你应该没问题。