I need some help with the code below.
What I’m trying to do is read data out of lines in a file.
The way I thought of doing this was to use a while loop to iterate through the lines to try and find a specific “x_y_z” and use the indexOf() method to see if it exists. So basically I want to run my loop until I get a value that isn’t -1, and then break the loop and return that value. I’m having trouble returning the value…I don’t seem to be able to get it out of the loop. Can anyone help me here?
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();
}
}
}
Thank you very much,
Tim
You code does not compile, the method does not return any value!
Do this instead:
Value will now contain the correct index, -1 otherwise.