2012-02-25 4 views
0

아래 코드에 도움이 필요합니다.
내가하려는 것은 파일의 행에서 데이터를 읽는 것입니다. 이 방법을 생각한 방법은 while 루프를 사용하여 특정 "x_y_z"를 찾고 해당 인덱스가 존재하는지 확인하기 위해 indexOf() 메서드를 사용하여 줄을 반복합니다. 그래서 기본적으로 -1이 아닌 값을 얻을 때까지 루프를 실행 한 다음 루프를 중단하고 그 값을 반환합니다. 값을 반환하는 데 문제가 있습니다 ... 루프에서 벗어날 수없는 것 같습니다. 누구든지 나를 도울 수 있습니까? Java로 파일을 검색하고 값을 반환하는 방법은 무엇입니까?

import java.io.InputStreamReader; 
import org.bukkit.entity.Player; 

/** 
* 
* @author Tim 
*/ 
public class ReadIn { 

    public static int read(String path, int x, int y, int z, Player player) { 
     try { 
      FileInputStream fstream = new FileInputStream(path); 
      DataInputStream in = new DataInputStream(fstream); 
      BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
      String strLine; 

      //Read File Line By Line 
      int index = -1; 
      while ((strLine = br.readLine()) != null && index == -1) { 
       index = strLine.indexOf(x + "_" + y + "_" + z); 
       if (index != -1) { 
        int value = index; 
        break; 
       } 
      } 

      //Close the input stream 
      in.close(); 

     } catch (Exception exception) {//Catch exception if any 
      exception.printStackTrace(); 
     } 
    } 
} 

은 매우

+0

이 컴파일됩니까? 나는 당신이 read 메쏘드로부터 값을 반환하고있는 것을 보지 못했다. –

+0

아니요, 컴파일되지 않습니다. 내 문제는 컴파일 할 때 return 문을 어디에 두어야하는지 묻는 것이 었습니다. 유감스럽게 생각하지 않았습니다. – Tim

답변

1

당신의 코드가 컴파일되지 않습니다 감사합니다, 어떤 값을 반환하지 않는 방법! 대신 다음을 수행하십시오.

public static int read(String path, int x, int y, int z, Player player) { 
     int value = -1; 
     try { 
      FileInputStream fstream = new FileInputStream(path); 
      DataInputStream in = new DataInputStream(fstream); 
      BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
      String strLine = "";  
      //Read File Line By Line 
      int index = -1; 
      while ((strLine = br.readLine()) != null) { 
       index = strLine.indexOf(x + "_" + y + "_" + z); 
       if (index != -1) { 
        value = index; 
        break; 
       } 
      } 
      //Close the input stream 
      in.close(); 
     } catch (Exception exception) {//Catch exception if any 
      exception.printStackTrace(); 
     } 
     return value; 
    } 

값에 올바른 색인 -1이 포함됩니다.

+0

고맙습니다. 나는 그 진술이 어디로 가야하는지 궁금합니다. – Tim

+0

예! 기쁜 일은 :) – GETah

0

나는 몇 가지 문제점을 안다. 우선,이 함수는 컴파일되지 않을 것이다. 둘째로, 문자열을 찾은 파일의 일부 줄의 위치에 해당하는 색인을 반환합니다. 전체 텍스트 줄을 반환하는 것이 더 좋지 않겠습니까? break를 사용하는 대신 값을 찾을 때 함수의 반환 값을 넣을 것입니다.

미안 이제 루프 내에서 돌아 오지 않는 이유를 알았습니다. 여러 종료 지점을 제거하기 위해 업데이트 - - 그러나 당신은 여전히 ​​당신이 당신의 기능에 마지막으로 넣으면

이 있다고 할 수

- 고정 변수 선언을 마침내의 범위가되도록 -

을 - - 좋아, 내가 지금 컴파일해야한다고 생각 -

public class ReadIn { 

public static int read(String path, int x, int y, int z, Player player) { 
    DataInputStream in = null; 
    int index = -1; 
    try { 
     FileInputStream fstream = new FileInputStream(path); 
     in = new DataInputStream(fstream); 
     BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
     String strLine; 
     //Read File Line By Line    
     while (index == -1 && (strLine = br.readLine()) != null) { 
      index = strLine.indexOf(x + "_" + y + "_" + z); 
     } 
     in.close(); 
    } catch (Exception exception) {//Catch exception if any 
     exception.printStackTrace(); 
    } 
    return index; 
} 
+0

내 목적을 위해 내가 원하는 것은 문자열을 찾은 위치의 위치입니다. 고마워, 내 질문은 반환 진술이 어디로 가야하는지에 관한 것이었다. – Tim

+0

아무런 문제없이 도움을 주셔서 감사합니다. 나는 마침내 학습을 들여다 볼 것입니다. – Tim

+0

@Matt Wolfe, 가능한 한 코드에서 여러 종료 점을 피하십시오. :) – GETah

0

이이 문제를 해결해야

public class ReadIn { 

    public static int read(String path, int x, int y, int z, Player player) { 
     try { 
      FileInputStream fstream = new FileInputStream(path); 
      DataInputStream in = new DataInputStream(fstream); 
      BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
      String strLine; 

      int value = -1; 
      //Read File Line By Line 
      int index = -1; 
      while ((strLine = br.readLine()) != null) { 
       index = strLine.indexOf(x + "_" + y + "_" + z); 
       if (index != -1) { 
        value = index; 
        break; 
       } 
      } 

      //Close the input stream 
      in.close(); 
      return value; 

     } catch (Exception exception) {//Catch exception if any 
      exception.printStackTrace(); 
     } 
     return -1; 
    } 
} 
관련 문제