Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using BufferedReader.readLine() in a while loop properly

So I'm having an issue reading a text file into my program. Here is the code:

try {
    InputStream fis = new FileInputStream(targetsFile);
    BufferedReader br = new BufferedReader(new InputStreamReader(fis));

    //while(br.readLine()!=null){
    for (int i = 0; i < 100; i++) {
        String[] words = br.readLine().split(" ");
        int targetX = Integer.parseInt(words[0]);
        int targetY = Integer.parseInt(words[1]);
        int targetW = Integer.parseInt(words[2]);
        int targetH = Integer.parseInt(words[3]);
        int targetHits = Integer.parseInt(words[4]);
        Target a = new Target(targetX, targetY, targetW, targetH, targetHits);
        targets.add(a);
    }
    br.close();
} catch (Exception e) {
    System.err.println("Error: Target File Cannot Be Read");
}

The file I am reading from is 100 lines of arguments. If I use a for loop it works perfectly. If I use the while statement (the one commented out above the for loop) it stops at 50. There is a possibility that a user can run the program with a file that has any number of lines, so my current for loop implementation won't work.

Why does the line while(br.readLine()!=null) stop at 50? I checked the text file and there is nothing that would hang it up.

I don't get any errors from the try-catch when I use the while loop so I am stumped. Anyone have any ideas?

like image 995
billg118 Avatar asked Nov 15 '12 20:11

billg118


People also ask

How does BufferedReader readLine work?

Class BufferedReader. Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.

How do I take BufferedReader input?

Reading User's Input using BufferedReader class:By wrapping the System.in (standard input stream) in an InputStreamReader which is wrapped in a BufferedReader, we can read input from the user in the command line. Here's an example: BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.

What is the use of BR readLine?

Java BufferedReader readLine() Method The readLine() method of Java BufferedReader class reads a line of text. The "/n" and "/r" are line termination character which is used to consider a line.

Does BufferedReader read line by line?

Java Read File line by line using BufferedReaderWe can use java. io. BufferedReader readLine() method to read file line by line to String. This method returns null when end of file is reached.


3 Answers

also very comprehensive...

try{     InputStream fis=new FileInputStream(targetsFile);     BufferedReader br=new BufferedReader(new InputStreamReader(fis));      for (String line = br.readLine(); line != null; line = br.readLine()) {        System.out.println(line);     }      br.close(); } catch(Exception e){     System.err.println("Error: Target File Cannot Be Read"); } 
like image 58
ramin Avatar answered Sep 28 '22 10:09

ramin


You're calling br.readLine() a second time inside the loop.
Therefore, you end up reading two lines each time you go around.

like image 35
SLaks Avatar answered Sep 28 '22 08:09

SLaks


You can use a structure like the following:

 while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
like image 25
Mona Jalal Avatar answered Sep 28 '22 10:09

Mona Jalal