0

I am trying to count line no. of a file using Java LineNumberReader. The output comes with problem. The problem is the alternative lines are displayed like line no. 1,3,5,... and on counting total no of lines i got half no. of the total actual lines. Here is the code

import java.lang.*;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

public class countLine{
    File file=null;
    public countLine(){
            file =new File("E:\\test.txt");
        getFileData();
    }
    public void getFileData(){
            try{ 
                if(file.exists()){
                    FileReader fr = new FileReader(file);
                    LineNumberReader lnr = new LineNumberReader(fr); 
                    int linenumber = 0;
                        do{
                System.out.println(lnr.readLine());
                        linenumber++;
                        }while (lnr.readLine() != null);
                        System.out.println("Total number of lines : " + linenumber);
                        lnr.close();
                }else{
                     System.out.println("File does not exists!");
                }
            }
        catch(Exception e){
            e.printStackTrace();
        }   
    }   
    public static void main(String h[]){
        countLine cl = new countLine(); 
    }
}
Harsh Kanakhara
  • 619
  • 2
  • 6
  • 20

4 Answers4

7

You read the line twice, once with System.out.println(lnr.readLine()); and once with while (lnr.readLine() != null);

Combining the two other answers into one gives correct line count as well as the ability of doing the System.out.println(...) with line content:

int linenumber = 0;
String tmp = new String();
while ((tmp = lnr.readLine()) != null) {
    linenumber++;
    System.out.println(tmp);
}
Michal
  • 1,883
  • 1
  • 11
  • 17
  • what is the proper way ? – Harsh Kanakhara Dec 31 '14 at 16:06
  • As one of the other answers suggested, depending on the fact whether you need the System.out.println of line content (Naor's answer) or not (laune's answer). – Michal Dec 31 '14 at 16:14
  • Yeah, that's ok. In my understanding your original code had one more bug, the do-while counted one line too much, if I see it correctly. But I did not unit-tested that assumption. – Michal Dec 31 '14 at 16:18
  • I am sorry I was too fast embracing the two answers. Naor's answer counts one line too much, you would need to initialize the linenumber with -1. – Michal Dec 31 '14 at 16:33
0

This would have been enough for counting:

FileReader fr = new FileReader(file);
LineNumberReader lnr = new LineNumberReader(fr); 
while (lnr.readLine() != null);
System.out.println( lnr.getLineNumber() );
lnr.close();

Added later Or, if you need to print lines (+ line numbers):

String line = null;
while ((line = lnr.readLine()) != null){
    System.out.println( lnr.getLineNumber() + " " +  lnr.getLineNumber() );
}
laune
  • 30,276
  • 3
  • 26
  • 40
0

You can get the number of lines with two lines of code: something like

lineNumberReader.skip(Long.MAX_VALUE);
int count = LineNumberReader.getLineNumber();

E&OE

user207421
  • 289,834
  • 37
  • 266
  • 440
-1

Every lnr.readLine() read a line from the file and return it.
You are making two reads:
1)At the System.out.println
2)At the while statement

You need make a call to readLine method,
save the result at variable and when it a null is the end of the file.

Take a look at:

int linenumber = 0;
String tmp = new String();
while ((tmp = lnr.readLine()) != null) {
    linenumber++;
    System.out.println(tmp);
}
Naor88
  • 50
  • 8
  • 1
    Why are you initializing `tmp` When it's assigned in the very next line? And what does 'if the file empty it's not count!!!' mean? And how many times are you going to answer this question? – user207421 Jan 01 '15 at 09:08
  • I'm rewrite my previous answer, it really don't be so understood sorry for that.. :) – Naor88 Jan 10 '18 at 16:36