I'm a novice in Java. I want to find the number of line in a test file so that I can use it for array size. What my initial thought was to create a loop and update an int variable. like this:
Scanner file = new Scanner(new File("data.text"));
int n = 0;
while(file.hasNextLine()) {
file.nextLine();
n++;
}
int[] array = new int[n];
this doesn't work for me because the scanner file is at the end of the text file. I won't be able to read the text file unless I close the original one and create a new scanner object. Is there a better way to execute this?