Java 中的 reached end of the file while parsing 錯誤

Mohammad Irfan 2023年1月30日 2022年5月1日
  1. reached end of the file while parsing-Java 中缺少類大括號
  2. reached end of the file while parsing-Java 中缺少 if 大括號
  3. reached end of the file while parsing-Java 中缺少迴圈大括號
  4. reached end of the file while parsing-Java 中缺少方法大括號
  5. 在 Java 中避免出現 reached end of the file while parsing 的錯誤
Java 中的 reached end of the file while parsing 錯誤

本教程介紹了在 Java 的程式碼編譯過程中出現的 reached end of the file while parsing 錯誤。

reached end of the file while parsing 錯誤是編譯時錯誤。當程式碼塊缺少大括號或程式碼中有額外的大括號時。

本教程將介紹此錯誤如何發生以及如何解決的不同示例。reached end of file while parsing 錯誤是編譯器告訴它已到達檔案末尾但未找到其末尾的方式。

在 Java 中,每個左大括號 ({) 都需要一個右大括號 (})。如果我們沒有在需要的地方放置大括號,我們的程式碼將無法正常工作,並且會出現錯誤。

reached end of the file while parsing-Java 中缺少類大括號

在下面的示例中,我們錯過了為類新增右大括號。

當我們編譯這段程式碼時,它會向控制檯返回一個錯誤。如果大括號的數量少於所需的數量,則會發生 reached 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 的右大括號。我們可以通過在程式碼末尾再新增一個大括號來解決這個問題。

看下面修改後的程式碼:

public class MyClass {
   static void print_something(){
     System.out.println("hello world");
   }
    public static void main(String args[]) {
  
      print_something();
    }  
  } 

輸出:

hello world

讓我們看一下可能發生此錯誤的示例。

reached end of the file while parsing-Java 中缺少 if 大括號

if 塊在下面的程式碼中缺少右大括號。這會導致在 Java 程式碼編譯期間出現 reached 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

reached end of the file while parsing-Java 中缺少迴圈大括號

缺少的大括號可能來自 whilefor 迴圈。在下面的程式碼中,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

reached 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 中避免出現 reached end of the file while parsing 的錯誤

這個錯誤很常見,也很容易避免。

為了避免這個錯誤,我們應該正確縮排我們的程式碼。這將使我們能夠輕鬆地找到丟失的右大括號。

我們還可以使用程式碼編輯器自動格式化我們的程式碼並將每個左大括號與其右大括號匹配。這將幫助我們找到右大括號缺失的地方。

相關文章 - Java Error