2014-02-06 7 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 문 앞에 선언하십시오.

복잡 [] complexArray = null; try 블록 내부의 실행이 complexArray가 범위를 벗어 완료되면 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); 
    } 
return complexArray; 
} 
catch(Exception e) { 
} 

}

+0

null로 선언하면 null로 반환됩니다. 어떤 이유로 배열이 try/catch 블록 내에서 변경되지 않는다. – user3277742

0

넣어 반환 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); 
      } 
     } //<<<=== 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 문 다음에 return 문이 없다는 메시지가 표시된다. – user3277742

+0

ok 그런 다음 return complexArray를 위치에 배치 한 다음 Complex [] complexArray = new Complex [ numOfComplex]; 아래 int numOfComplex = 0; –

0

+0

그것은 변수가 초기화되지 않을 것이라고 말합니다 --- 나는 일어나고있는 일이 배열에 값을 넣지 않는다고 생각하며 왜 그런지 모르겠다. – user3277742

0

당신은 새로운 복잡한 배열을 만들어 complexArray 개체를 초기화 할 필요가

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 

@hemanth에서에 계속하려고합니다. 그것이 당신 문제입니다.

나는 이것이 효과가있을 것이라고 생각합니다. 모두 잘되면 try 절을 입력해야합니다. 그렇지 않은 경우 크기가 0 인 complexArray가 반환됩니다.이 경우 파일에 문제가 발생하면 수동으로 수행해야하는 작업이 필요합니다.

+0

작동하지 않는다. 이유는 내가 가져야하기 때문이다. 내가 읽고있는 파일에서 배열의 길이, 따라서 파일에서 읽기 전에 크기로 배열을 선언 할 수 없습니다. 하지만 try/catch 블록에서 아무 일도하지 않으면 배열에 전혀 영향을 미치지 않거나 배열에 영향을주지 않는 것 같습니다. – user3277742

+0

ok, return 절을 try 절 안에 넣지 마십시오. – SeekingAlpha

+0

그것은 try/catch 후에 return 문이 필요하다는 것을 알려주기 때문입니다. 마치 try 절로 들어가는 것조차 보이지 않는 것 같습니다. – user3277742