1

why doesn´t if (txtLine == null) { break; };work? or maybe the correct answer is why does it still set the string txtLine to null (literally). The way I understand it, it should break the moment the string is null? I don´t want it to set the string to "null". but stop when there are no more lines in the *.txt file

try{
    BufferedReader txtReader = new BufferedReader (new FileReader ("test.txt"));
    while (true) {
        // Reads one line.
        println(txtLine);
        if(txtLine == null){
            break;
        };
        txtLine = txtReader.readLine();
        nLines(txtLine);
    }
    txtReader.close();
} catch (IOException ex) {
    throw new ErrorException(ex);   
}

the txtFile variable is defined as an IVAR

private int nChars = 0;
private String txtLine = new String(); 
private ArrayList <String> array = new ArrayList <String>();
6
  • 2
    Where is txtLine defined ? Can you please add it to your code snippet? Commented Sep 30, 2012 at 17:24
  • If you google about reading user input through Scanner or BufferedReader in Java -> You will immediately get millions of result which will clear your doubt.. Commented Sep 30, 2012 at 17:27
  • I advice you to always have finally when working with IO Commented Sep 30, 2012 at 17:29
  • BTW, you could move txtLine = txtReader.readLine(); before if(txtLine == null) and println(txtLine) after if(txtLine == null) Commented Sep 30, 2012 at 17:29
  • @Lews: nLines just adds the line to an Array then returns it´s size Commented Sep 30, 2012 at 17:30

1 Answer 1

4

I think the ordering of when you break and when you change the value of txtLine to be the next line read from the file is backwards, your code should look something like:

try{
    BufferedReader txtReader = new BufferedReader (new FileReader ("test.txt"));
    while (true) {
        // Reads one line.
        println(txtLine);
        txtLine = txtReader.readLine();
        // check after we read the value of txtLine
        if(txtLine == null){
            break;
        }

        nLines(txtLine);
    }
    txtReader.close();
} catch (IOException ex) {
    throw new ErrorException(ex);   
}

But this is a much more concise (and I think, clearer) form:

try{
    BufferedReader txtReader = new BufferedReader (new FileReader ("test.txt"));
    while ((txtLine = txtReader.readLine()) != null) {
        // Reads one line.
        println(txtLine);
        nLines(txtLine);
    }
    txtReader.close();
} catch (IOException ex) {
    throw new ErrorException(ex);   
}

Where while ((txtLine = txtReader.readLine()) != null) sets txtLine to the next line, and then checks that txtLine is not null before continuing.

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

1 Comment

Correct! it was the ordering of txtLine = txtReader.readLine();as you pointed out. Of course it needs to read the new Line to check if it is null before performing a conditional check.

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.