I was trying to read lines of text from a java file and the while loop I was trying to use never ran (e.g. readText.hasNextLine() evaluated to false). I tried to do the same thing with a different file and it worked that time, but when I added a longer line of text to the file it didn't work anymore. Is there a limit on the length of lines that a Scanner can read? Why does it seem like my file no longer has text in it? Here is my code:
import java.util.*;
import java.io.*;
public class errorSolve{
public static void main(String[] args) throws FileNotFoundException{
Scanner readText = new Scanner (new File("try.txt"));
while(readText.hasNextLine()){
System.out.println(readText.nextLine());
}
}
}
Before I added the while loop I got this error:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at errorSolve.main(errorSolve.java:7)