我得到一個錯誤,說編譯器找不到我的變量「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;
}
當我將它聲明爲null時,它返回爲null。由於某種原因,數組在try/catch塊內部沒有發生變化 – user3277742