1

このメソッドを使用すると、このエラーが発生し続けます java.util.NoSuchElementExceptionNo line found

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 + "'");
    }
  }
}

デバッガーを使用すると、文字通りファイル全体が処理され、終了時にエラーが表示されます。

4

2 に答える 2

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 をテストするだけでは、最後の行がコメント/空白の場合、ループを終了することはないと思います。

于 2014-11-03T09:55:56.887 に答える
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())ください。大丈夫だと思います。

于 2014-11-03T08:37:39.353 に答える