Den - Ben
Den - Ben

Reputation: 99

Checking for end of file does not work as expected

I try to check if I have reached the end of my file, but the condition with empty string does not work

public void readEquation(String fileName) {  
        File aFile = new File(fileName);
        String line = null;
        int cnt = 0;
        try {
            FileReader reader = new FileReader(aFile);
            BufferedReader bufferedReader = new BufferedReader(reader);
            while ((line = bufferedReader.readLine()) != null) {      
                //but line gets null
                intoMap(line, cnt);
                cnt++;
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

Upvotes: 0

Views: 252

Answers (5)

Pshemo
Pshemo

Reputation: 124275

while ((bufferedReader.readLine()) != null) {      
                line = bufferedReader.readLine();    //but line gets null

You read two lines here.

To avoid that use tmp String like

String line=null;
while ((line=bufferedReader.readLine()) != null) {  
    //now line stores line from file or null if file doesn't have new line

Edit - example

I just tested this code

    BufferedReader bufferedReader=null;
    try {
        String line = null;
        bufferedReader = new BufferedReader(new FileReader("d:\\ranks.txt"));
        while ((line=bufferedReader.readLine()) != null) {      
            System.out.println(line);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally{
        try {
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

and it worked.

Upvotes: 0

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33544

This will work for you.

 while ((line = bufferedReader.readLine()) != null) {      

                    intoMap(line, cnt);
                    cnt++;
                }

Upvotes: 0

Tudor
Tudor

Reputation: 62469

You are erroneously reading two lines instead of one a time. The condition of the loop reads a line by itself and then you read another one inside the loop. I suggest this change:

public void readEquation(String fileName) {  
        File aFile = new File(fileName);
        String line = null;
        int cnt = 0;
        try {
            FileReader reader = new FileReader(aFile);
            BufferedReader bufferedReader = new BufferedReader(reader);
            line = bufferedReader.readLine();
            while (line != null) {                  
                intoMap(line, cnt);
                cnt++;
                line = bufferedReader.readLine();
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

Upvotes: 1

Lai Xin Chu
Lai Xin Chu

Reputation: 2482

The problem with your code is that when you execute bufferedReader.readLine() in your while condition, you are already reading the line.

You should do this:

public void readEquation(String fileName) {  
    File aFile = new File(fileName);
    String line = null;
    int cnt = 0;
    try {
        FileReader reader = new FileReader(aFile);
        BufferedReader bufferedReader = new BufferedReader(reader);
        while ((line = bufferedReader.readLine()) != null) {       // only reads once
            intoMap(line, cnt);
            cnt++;
        }
    } catch (IOException e1) {
        e1.printStackTrace();
    }
}

Upvotes: 0

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81734

You're reading two lines in succession: one in the loop condition, then another in the body of the loop. The one you read in the condition is ignored and never used. Nothing ever checks the value of the second line.

Usually people write this loop this way:

while ((line = bufferedReader.readLine()) != null) {      
    intoMap(line, cnt);
    cnt++;
}

This way you always check the line before using it.

Upvotes: 0

Related Questions