The scanner in the hasNext () text file is infinite

I am writing a simple Java program, and it requires reading data from a text file. However, I have problems counting strings. This problem seems to be generalized enough for a simple Google search, but I can’t even find the right things.

The tutorial I'm studying suggests that in order to count the number of lines in a text file, you should do something like this:

public static int[] sampleDataArray(String inputFile) throws IOException { File file = new File(inputFile); Scanner inFile = new Scanner(file); int count = 0; while (inFile.hasNext()) count++; int[] numbersArray = new int[count]; inFile.reset(); for (int i = 0; i < count; i++) { numbersArray[i] = inFile.nextInt(); } inFile.close(); return numbersArray; } 

It seems to me that the while (inFile.hasNext()) line while (inFile.hasNext()) is the problem. I think hasNext() works endlessly. The data file that I use with the code definitely has a finite number of data lines.

What should I do?

+4
source share
4 answers

After calling hasNext() for the first time, if you do not read from the file hasNext() will always return true. Since the front of the input is not changed.

Imagine you have a file with this line:

introduced

If you call hasNext() in this file, it will return true because the file has the next token, in this case the word this .

If you do not read the file after this initial call, the β€œnext” input to be processed, STILL, is the word this . The next input does not change until you read from the file.

TL DR

When you call hasNext() read from a file, otherwise you will always have an infinite loop.

Additionally

If you really want to use hasNext() or you want, you can create another Scanner object and read the file to count the lines, then your loop will work fine. Also, you really should use hasNextLine()

 public int countLines(File inFile) { int count = 0; Scanner fileScanner = new Scanner(inFile); while(fileScanner.hasNextLine()) //if you are trying to count lines { //you should use hasNextLine() fileScanner.nextLine() //advance the inputstream count++; } return count; } 

Hope this will be helpful.

+9
source

inFile.hasNext() does not move the pointer to the next line

try it

 String x=null; while((x = inFile.next()) != null) count++; 

hasNext () description

Returns true if this scanner has a different token at its input. This method may block while waiting for input to be scanned. The scanner does not move past any input.

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#hasNext%28%29

+8
source

To count the number of lines, use hasNextLine() instead of hasNext() .
In a while you should call nextLine() , because in your current implementation, the scanner is static on the first line. Calling this method will force it to move to the next line, at each iteration of the loop.

Refer to the following code snippet:

 while (inFile.hasNextLine()){ inFile.nextLine() count++; } 
+2
source

You cannot use the scanner to count the number of lines in a file, since the default scanner uses free space to separate tokens. I would suggest using the BufferReader and readline method.

http://docs.oracle.com/javase/6/docs/api/java/io/BufferedReader.html

 private Integer getNoOfLines( String fileName) throws FileNotFoundException, IOException { FileInputStream fstream = new FileInputStream(fileName); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; List<String> lineList = new ArrayList<String>(); while ((strLine = br.readLine()) != null) { lineList.add(strLine); } in.close(); return lineList.size(); } 
+1
source

Source: https://habr.com/ru/post/1384252/


All Articles