Home > Software engineering >  Java hasNextLine() method
Java hasNextLine() method

Time:01-13

I am having a problem with the Scanner class method hasNextLine() (or the hasNext() method). Basically, I am trying to read from a text file that has a list of integer values. I need to read through the text file and store the values that are there in an array (not an arrayList). The code below first goes through the text file to "see" how many values are there. I'm doing this because I can't think of another way to count the total number of integers that are in the text file, and I need to know how long my array has to be.

That being said, once I do that it seems that the hasNextLine() method (or the hasNext() method) "stays" at the bottom of the text file once I loop through:

import java.util.Scanner;
import java.io.*; 
import java.util.*; 

public class Main{
  public static void main(String[] args) throws IOException
  {
    int y = 0; //stores numbers from the text file below
    int counter = 0; //stores the number of datapoints in the text file to read from 
    
    File f = new File("Test Data.txt");// load an external file into a variable of type File. 
                 
    Scanner reader = new Scanner(f);//instead of using System.in use f. 
        
    while (reader.hasNext()) // this will read the file's contents line-by-line
        {
            y = reader.nextInt(); 
            counter  ;/*stores the total number of integers in the Test Data.txt file so I can 
                        know how long my array that stores the numbers from the txt file needs 
                        to be.*/
        }//ends loop 
        System.out.println("YOU HAVE "   counter   " DATA POINTS IN THE FILE");
        
        int [] myNumbers = new int[counter]; //will store the integers from a data file

        for (int i = 0; i < counter; i  ){
            if (reader.hasNext()){
                System.out.println(i); 
                myNumbers[i] = reader.nextInt();
            }//ends if statement
            
        }//ends loop filling array
        
        
        reader.close(); 
    }
}

Is there a way to send the scanner "back to the top" of the text file WITHOUT creating a new scanner object? I know I could just create a new Scanner object, and then just loop and store each data point in an array, but it seems like there should be another way to do what I need to do. Is there? The documentation for the method in question doesn't mention much detail. I tried using the reset() method but that did not work.

I am not using an ArrayList because of a condition of the project I am working on. I understand that I could use an arrayList and not have to worry about counting the number of data values in the text file. However, the student I am helping has not learned about arrayLists yet as his class does not include them in the beginner course he's taking.

CodePudding user response:

When reading the file for the second pass, just dispose the old scanner and get a new one. It is all in one line:

reader = new Scanner(f);

This will overwrite the reader with a reference to a new Scanner, one that reads from the beginning of the file. The old Scanner instance, which is no longer accessible will automatically be cleared by the garbage collector.

  • Related