Reputation: 638
Below is my current function. It works fine and returns true if the lineToCompare is found.
public static int checkrank(String lineToCompare){
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("ranks.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
//Compare the line with the line to compare (string)
if(strLine.trim().compareTo(lineToCompare) == 0)
return true;
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
return false;
}
However, I need it to get the line AFTER the lineToCompare is found.
Here's an example text file:
xXPwnageLolXx
1
SomethingTest
0
AnotherSomething
3
I need it to find the line of text (lineToCompare), then return the number AFTER the line found. How would I do this?
Upvotes: 2
Views: 572
Reputation: 38541
while ((strLine = br.readLine()) != null) {
//Compare the line with the line to compare (string)
if(strLine.trim().compareTo(lineToCompare) == 0) {
//I found it! Read the next line...
final String lineAfter = br.readLine();
return Integer.parseInt(lineAfter);
}
}
I've edited this snippet to include Dowards suggestion. The local variable is unnecessary, I'm just leaving it in for readability.
Upvotes: 5