2012-02-25 18 views
0

私は以下のコードで助けが必要です。
私がしようとしているのは、ファイル内の行からデータを読み出すことです。 私はこれを行う方法は、特定の "x_y_z"を探してindexOf()メソッドを使用してそれが存在するかどうかを調べるためにwhileループを繰り返し使用することでした。だから基本的に私は-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

私はいくつかの問題があることを見ています。まず、あなたの関数には戻りませんので、これはコンパイルされません。 もう1つは、その文字列を見つけたファイル内のある行の位置に対応するインデックスを返します。テキスト全体を返す方が良いのではないでしょうか? 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; 
    } 
} 
関連する問題