0

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)

2 Answers2

0

I tested here and it worked, man. And an object of type Scanner has no row limit. I tested with a 100Mb text file.

A similar doubts: here

Community
  • 1
  • 1
0

Okay so I figured out that the reason this failed wasn't a failure in the code it was because my file had the wrong kind of quotation marks. Apparently, if you copy and paste text from Microsoft Word they use a different kind of quotation marks which throws off the Scanner because they're not ASCII code characters. (” vs ")