Hi I have a fairly simple program but I am having trouble understanding why I have an inifite loop when I am running it. The file I am reading from has 10 integers in it. I am using Eclipse Juno and the output in the console is counting by 1 starting at 281363 infinitely. How can I fix this? Thanks in advance.
import java.util.*;
import java.io.*;
public class TestScoreAnalyzer
{
public static void main(String[] args) throws FileNotFoundException
{
int arraySize = 0;
File file = new File("C:\\Users\\Quinn\\workspace\\CPS121\\src\\
additionalAssignments\\scoresSample.txt");
Scanner inputFile = new Scanner(file);
while(inputFile.hasNextInt())
{
arraySize++;
System.out.println(arraySize);
}
inputFile.close();
}
}
You’re never calling
inputFile.nextInt()– you’re only callinghasNextInt(), which doesn’t actually advance the location in the file. You probably want: