2014-02-06 155 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