-3

Sorry for the noob question, but I feel like my code is correct but I can't see why the read won't go to the next line:

This is my code so far:

BufferedReader buffer = null;

try {
    FileReader file = new FileReader(filename);
    buffer = new BufferedReader(file);

    String line = buffer.readLine();
    String[] separations = line.split(", ");
    System.out.println(separations[0]);
    while(line!= null) {
        this.times.add(separations[0]);

        Double number = Double.parseDouble(separations[1]);
        allNumbers.add(number);

        line = buffer.readLine();
    }
rjdkolb
  • 10,377
  • 11
  • 69
  • 89
Adrian Coutsoftides
  • 1,203
  • 1
  • 16
  • 38
  • You feel like you code is correct? It's a binary decision. Is your code correct? Does it work? It sounds like it does not work. First, are you getting any exceptions? What are they? What does your exception handling code look like? Have you verified that `file` actually exists? Are you using a full path, a relative path? – Elliott Frisch Jan 30 '18 at 03:55
  • Possible duplicate of [How to read a large text file line by line using Java?](https://stackoverflow.com/questions/5868369/how-to-read-a-large-text-file-line-by-line-using-java) – USER249 Jan 30 '18 at 03:58
  • @NathanHughes No more code is needed other than what I have provided, as my title suggests, the output to console shows that the the subroutine is only reading the first line and is not progressing to the next line; therefore the must be an issue with the way I've constructed the if statement; the errors that are caught are the usual suspects when dealing withe files, FileNotFound and IOExceptions – Adrian Coutsoftides Jan 30 '18 at 04:00
  • @ElliottFrisch clearly, i meant it as a turn of phrase, my code does not work otherwise it would not be posted here; in regards to exceptions, none are thrown, please refer to my reply to Nathan Hudges – Adrian Coutsoftides Jan 30 '18 at 04:01

1 Answers1

-1

The issue was not that the buffer was only reading one line but I was tokenizing the line outside of the loop, so the same information got stored for x number of lines:

Solution:

while (line != null) {
    String[] separations = line.split(", "); // this should be inside the loop
    if (separations.length > 1) {
        this.times.add(separations[0]);

        Double number = Double.parseDouble(separations[1]);
        allNumbers.add(number);
    }
    line = buffer.readLine();
}
rjdkolb
  • 10,377
  • 11
  • 69
  • 89
Adrian Coutsoftides
  • 1,203
  • 1
  • 16
  • 38