Jacob O
Jacob O

Reputation: 53

Read from file with BufferedReader

Basically I've got an assignment which reads multiple lines from a .txt file. There are 4 values in the text file per line and each value is separated by 2 spaces. There are about 10 lines of data in the file.

After taking the input from the file the program then puts it onto a Database. The database connection functionality works fine.

My issue now is with reading from the file using a BufferedReader. The issue is that if I uncomment any 1 of the 3 lines at the bottom the BufferedReader reads every other line. And if I don't use them then there's an exception as the next input is of type String. I have contemplated using a Scanner with the .hasNextLine() method.

Any thoughts on what could be the problem and how to fix it? Thanks.

File file = new File(FILE_INPUT_NAME);
FileReader fr = new FileReader(file);
BufferedReader readFile = new BufferedReader(fr);
String line = null;
while ((line = readFile.readLine()) != null) {
    String[] split = line.split("  ", 4);

    String id = split[0];
    nameFromFile = split[1];
    String year = split[2];
    String mark = split[3];

    idFromFile = Integer.parseInt(id);
    yearOfStudyFromFile = Integer.parseInt(year);
    markFromFile = Integer.parseInt(mark);

    //line = readFile.readLine();
    //readFile.readLine();
    //System.out.println(readFile.readLine());
}

Edit: There was an error in the formatting of the .txt file. a missing value. But now I get an ArrayOutOfBoundsException.

Edit edit: Another error in the .txt file! Turns out there was a single space instead of a double. It seems to be working now. But any advice on how to deal with file errors like this in the future?

Upvotes: 2

Views: 5919

Answers (2)

user207421
user207421

Reputation: 311050

The issue is that if I uncomment any 1 of the 3 lines at the bottom the BufferedReader reads every other line.

Correct. If you put any of those lines of code in, the line of text read will be thrown away and not processed. You're already reading in the while condition. You don't need another read. If you put any of those lines in, they will be thrown away and not proce

Upvotes: 1

PNS
PNS

Reputation: 19915

A compilable version of the code posted could be

  public void read() throws IOException {
    File file = new File(FILE_INPUT_NAME);
    FileReader fr = new FileReader(file);
    BufferedReader readFile = new BufferedReader(fr);
    String line;
    while ((line = readFile.readLine()) != null) {
      String[] split = line.split(" ", 4);
      if (split.length != 4) { // Not enough tokens (e.g., empty line) read
        continue;
      }
      String id = split[0];
      String nameFromFile = split[1];
      String year = split[2];
      String mark = split[3];

      int idFromFile = Integer.parseInt(id);
      int yearOfStudyFromFile = Integer.parseInt(year);
      int markFromFile = Integer.parseInt(mark);

      //line = readFile.readLine();
      //readFile.readLine();
      //System.out.println(readFile.readLine());
    }
  }

The above uses a single space (" " instead of the original " "). To split on any number of changes, a regular expression can be used, e.g. "\\s+". Of course, exactly 2 spaces can also be used, if that reflects the structure of the input data.

What the method should do with the extracted values (e.g., returning them in an object of some type, or saving them to a database directly), is up to the application using it.

Upvotes: 0

Related Questions