2014-02-06 198 views
0

我得到一個錯誤,說編譯器找不到我的變量「complexArray」,但我不知道爲什麼。如何修復我的程序,使其返回從文件中讀取的複數數組?Java:局部變量範圍

public static Complex[] parseFromFile(String fileName) { 
    int numOfComplex = 0; 
    try { 
     Scanner sc = new Scanner(new File(fileName)); 
     String firstLine = sc.nextLine(); 
     firstLine = firstLine.trim(); 
     numOfComplex = Integer.parseInt(firstLine); 
     Complex[] complexArray = new Complex[numOfComplex]; 
     for (int i = 0; i < numOfComplex; i++) { 
      String nextLine = sc.nextLine(); 
      nextLine = nextLine.trim(); 
      complexArray[i] = parseComplex(nextLine); 
     } 
    } 
    catch(Exception e) { 
    } 
    return complexArray; 
} 

回答

0

您的complexArray變量在try {}範圍內聲明。在try語句之前聲明它。

Complex [] complexArray = null;因爲complexArray在try {}語句

public static Complex[] parseFromFile(String fileName) { 
int numOfComplex = 0; 
try { 
    Scanner sc = new Scanner(new File(fileName)); 
    String firstLine = sc.nextLine(); 
    firstLine = firstLine.trim(); 
    numOfComplex = Integer.parseInt(firstLine); 
    Complex[] complexArray = new Complex[numOfComplex]; 
    for (int i = 0; i < numOfComplex; i++) { 
     String nextLine = sc.nextLine(); 
     nextLine = nextLine.trim(); 
     complexArray[i] = parseComplex(nextLine); 
    } 
return complexArray; 
} 
catch(Exception e) { 
} 

}

+0

當我將它聲明爲null時,它返回爲null。由於某種原因,數組在try/catch塊內部沒有發生變化 – user3277742

0

認沽回報complexArray語句在try塊中聲明,一旦try塊內執行完畢complexArray超出範圍。

 public static Complex[] parseFromFile(String fileName) { 
     int numOfComplex = 0; 
     try { 
      Scanner sc = new Scanner(new File(fileName)); 
      String firstLine = sc.nextLine(); 
      firstLine = firstLine.trim(); 
      numOfComplex = Integer.parseInt(firstLine); 
      Complex[] complexArray = new Complex[numOfComplex]; 
      for (int i = 0; i < numOfComplex; i++) { 
       String nextLine = sc.nextLine(); 
       nextLine = nextLine.trim(); 
       complexArray[i] = parseComplex(nextLine); 
      } 
     } //<<<=== scope of `complexArray` ends here 
     catch(Exception e) { 
     } 
     return complexArray; 
    } 

試試這個

 public static Complex[] parseFromFile(String fileName) { 
     int numOfComplex = 0; 
     Complex[] complexArray; 
     try { 
      Scanner sc = new Scanner(new File(fileName)); 
      String firstLine = sc.nextLine(); 
      firstLine = firstLine.trim(); 
      numOfComplex = Integer.parseInt(firstLine); 
      complexArray = new Complex[numOfComplex]; 
      for (int i = 0; i < numOfComplex; i++) { 
       String nextLine = sc.nextLine(); 
       nextLine = nextLine.trim(); 
       complexArray[i] = parseComplex(nextLine); 
      } 
     } 
     catch(Exception e) { 
     } 
     return complexArray; 
    } //<<<=== scope of `complexArray` ends here 
+0

這說明我在catch – user3277742

+0

ok後丟失了返回語句,然後嘗試將返回complexArray放在它所在的位置,然後將Complex [] complexArray = new Complex [ numOfComplex];低於int numOfComplex = 0; –

0

+0

它說變量可能不會被初始化---我認爲發生了什麼是它不會將值放入數組中,我不知道爲什麼 – user3277742

0

從@hemanth

public static Complex[] parseFromFile(String fileName) { 
     int numOfComplex = 0; 
     Complex[] complexArray = new Complex[numOfComplex]; // Need to initialize the array 
     try { 
      Scanner sc = new Scanner(new File(fileName)); 
      String firstLine = sc.nextLine(); 
      firstLine = firstLine.trim(); 
      numOfComplex = Integer.parseInt(firstLine); 
      complexArray = new Complex[numOfComplex]; 
      for (int i = 0; i < numOfComplex; i++) { 
       String nextLine = sc.nextLine(); 
       nextLine = nextLine.trim(); 
       complexArray[i] = parseComplex(nextLine); 
       } 
      return complexArray; 
     } 
     catch(Exception e) { 
      System.out.println("Exception was thrown"); // try adding this to see if an exception is thrown. 
     } 
     return complexArray; 
    } //<<<=== scope of `complexArray` ends here 

你需要創建一個新的複雜陣列初始化complexArray對象在繼續。這是你的問題。

我認爲這可能有效。如果一切正常,應該輸入try子句。如果不是,它將返回大小爲0的複數陣列。我假設如果發生這種情況,文件有問題,這意味着您需要手動執行某些操作。

+0

也不起作用 - 原因是因爲我必須得到從我正在閱讀的文件中讀取數組的長度,因此在從文件中讀取數據之前,我無法用它的大小聲明數組。但是當我在try/catch塊中做任何事時,它似乎沒有改變或影響數組,所以 – user3277742

+0

好吧,爲什麼不把return語句放在try子句中呢? – SeekingAlpha

+0

因爲它告訴我,我需要try/catch後的return語句。它似乎甚至沒有進入try子句。 – user3277742