1

I have this function:

public static int checkRank(String lineToCompare){
    int rank = 0;
    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
        System.out.println("SPOT 0");
        while ((strLine = br.readLine()) != null)   {
            //Compare the line with the line to compare (string)
            System.out.println("SPOT 1");
            if(strLine.trim().equals(lineToCompare)) {
                //I found it!  Read the next line...
                final String lineAfter = br.readLine();
                rank = Integer.parseInt(lineAfter);  
                System.out.println("SPOT 2");
            }else{
                rank = 0;
                System.out.println("SPOT 3");
            }
        }

        //Close the input stream
        in.close();
    }catch (Exception e){//Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }

    System.out.println("Username: " + lineToCompare + " | Rank: " + rank);
    return rank;
}

My ranks.txt file is as follows:

test1
2
test2
1

The console outputs (the "SPOT"s were just used for debugging):

[Commands] SPOT 0
[Commands] SPOT 1
[Commands] SPOT 2
[Commands] SPOT 1
[Commands] SPOT 3
[Commands] SPOT 1
[Commands] SPOT 3
[Commands] Username: test1 | Rank: 0

As you can see, if it were working correctly it would say test1 has a rank of 2. Can someone help me identify the problem?

6
  • What is lineToCompare? Commented May 15, 2012 at 0:27
  • @Jeffrey, lineToCompare = test1 (as it shows in the console's output). Commented May 15, 2012 at 0:27
  • 2
    Try adding a break; after printing SPOT2. You set the rank to 0 after you read "test2" and then again when you read "1". Commented May 15, 2012 at 0:33
  • @DannyF247 Aha! The code box has a scroll bar. Sorry about that Commented May 15, 2012 at 0:33
  • @ppalasek Thanks that fixed it!! If you want to leave that as an answer so I can accept it and get you rep, I'd be more than happy to :) Commented May 15, 2012 at 0:35

2 Answers 2

1

You should add a break; statement after the line where you print out "SPOT 2" to end the while loop.

Without the break you set the rank to 0 when you read the line "test2" and then again when you read the line "1" (the while loop continues until the end of file).

Sign up to request clarification or add additional context in comments.

Comments

1

You are overwriting rank with 0 in the last loop iteration.

Break out of the loop using break; when you get a hit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.