I think I'm doing something really silly, but I'm new to Java so bear with me. I'm using a FileReader and Scanner to read through the lines of a .txt file. After grabbing each line, I extract some information out and then move the line. This is what my simplified method looks like:
Reader fileReader = new FileReader("test.txt");
Scanner scanner = new Scanner(fileReader);
public static textList(Scanner scanner){
while (scanner.hasNextLine()){
scanner.nextLine();
while(scanner.hasNext()){
// Extract information from each line and do some stuff with it
// using scanner.next();
}
}
}
// scanner is closed in the same method that creates it and the FileReader.
My problem is, if I leave the scanner.nextLine()
where it is, I'll always skip the first line of the .txt file, but if I move it to the end of the while( scanner.hasNextLine())
, I get a "no such line exists" exception when the scanner reaches the end of the .txt file.
Any help and guidance would be gratefully received!
Thanks