Java での Reach End of File While Parsing Error の修正について
- 
          
            reach end of the file while parsing-Java でクラス中括弧がありません
- 
          
            reach end of the file while parsing-Java で中括弧がない場合
- 
          
            reach end of the file while parsing-Java でループ中括弧がありません
- 
          
            reach end of the file while parsing-Java でメソッド閉じ中括弧がありません
- 
          
            Java での reach end of the file while parsingエラーを回避する
 
このチュートリアルでは、Java でのコードコンパイル中に reach end of the file while parsing というエラーが発生します。
reach end of the file while parsing エラーは、コンパイル時のエラーです。コードブロックに中括弧がない場合、またはコードに余分な中括弧が含まれている場合。
このチュートリアルでは、このエラーがどのように発生し、どのように解決するかについて、さまざまな例を見ていきます。reach end of the file while parsing エラーは、ファイルの終わりに到達したが、ファイルの終わりが見つからないことをコンパイラが通知する方法です。
Java では、すべての開始中括弧({)には終了中括弧(})が必要です。必要な場所に中括弧を付けないと、コードが正しく機能せず、エラーが発生します。
reach end of the file while parsing -Java でクラス中括弧がありません
以下の例では、クラスに中括弧を追加できませんでした。
このコードをコンパイルすると、コンソールにエラーが返されます。中括弧の数が必要な数より少ない場合、reach end of the file while parsing エラーが発生します。
以下のコードを見てください。
public class MyClass {
  public static void main(String args[]) {
    print_something();
  }
出力:
MyClass.java:6: error: reached end of file while parsing
    }
     ^
1 error
上記のコードには、MyClass の閉じ中括弧がありません。この問題は、コードの最後にもう 1つの中括弧を追加することで解決できます。
以下の変更されたコードを見てください。
public class MyClass {
  static void print_something() {
    System.out.println("hello world");
  }
  public static void main(String args[]) {
    print_something();
  }
}
出力:
hello world
このエラーが発生する可能性のある例を見てみましょう。
reach end of the file while parsing -Java で中括弧がない場合
if ブロックには、以下のコードで閉じ中括弧がありません。これにより、Java でのコードコンパイル中にreach end of the file while parsing エラーが発生します。
public class MyClass {
  public static void main(String args[]) {
    int x = 38;
    if (x > 90) {
      // do something
      System.out.println("Greater than 90");
    }
  }
出力:
MyClass.java:8: error: reached end of file while parsing
}
 ^
1 error
このエラーは、適切な場所(if ブロックの最後)に中括弧を追加することで解決できます。以下のコードを見てください。
public class MyClass {
  public static void main(String args[]) {
    int x = 38;
    if (x > 90) {
      // do something
      System.out.println("Greater than 90");
    } // this brace was missing
  }
}
上記のコードはエラーなしでコンパイルされます。
出力:
Greater than 90
reach end of the file while parsing -Java でループ中括弧がありません
欠落している中括弧は、while または for ループからのものである可能性があります。以下のコードでは、while ループブロックに必要な閉じ中括弧がないため、コンパイルが失敗します。
以下の例を参照してください。
public class MyClass {
  public static void main(String args[]) {
    int x = 38;
    while (x > 90) {
      // do something
      System.out.println("Greater than 90");
      x--;
    }
  }
出力:
MyClass.java:10: error: reached end of file while parsing
}
 ^
1 error
このエラーは、中括弧を必要な位置(while ループの最後)に配置することで解決できます。以下の変更されたコードを見てください。
public class MyClass {
  public static void main(String args[]) {
    int x = 38;
    while (x > 90) {
      // do something
      System.out.println("Greater than 90");
      x--;
    } // This brace was missing
  }
}
上記のコードはエラーなしでコンパイルされます。
出力:
Greater than 90
reach end of the file while parsing -Java でメソッド閉じ中括弧がありません
この場合、閉じ中括弧が欠落しているメソッドを定義しました。このコードをコンパイルすると、コンパイラエラーが発生します。以下のコードを見てください。
public class MyClass {
  public static void main(String args[]) {
    customFunction();
  }
  static void customFunction() {
    System.out.println("Inside the function");
  }
出力:
MyClass.java:9: error: reached end of file while parsing
}
 ^
1 error
このエラーは、中括弧を必要な位置(関数本体の端)に配置することで解決できます。以下の変更されたコードを見てください。
public class MyClass {
  public static void main(String args[]) {
    customFunction();
  }
  static void customFunction() {
    System.out.println("Inside the function");
  }
}
出力:
Inside the function
Java での reach end of the file while parsing エラーを回避する
このエラーは非常に一般的であり、回避するのは非常に簡単です。
このエラーを回避するには、コードを適切にインデントする必要があります。これにより、不足しているクロージングカーリーブレースを簡単に見つけることができます。
コードエディタを使用して、コードを自動的にフォーマットし、各開始中括弧をその終了中括弧と一致させることもできます。これは、閉じ中括弧が欠落している場所を見つけるのに役立ちます。