Fizzy Fizzy -4 years ago 100
Java Question

My program is stuck in a while loop reading from a file

public static void main(String[] args) throws IOException{
Scanner fileReader = new Scanner(new File("EmployeeInfo.txt"));

int year = 2014;
//int year = fileReader.nextInt();
if(year == 2014){


}//end if

}// end while loop

}//end main method

im not sure why it is stuck in the while loop. i have verified that the program opens the file and can read it.

Answer Source

That's because you are not calling nextLine() (or nextXXX()) method inside the while loop which means the Scanner never advances to the next token.

Here's what the documentation of nextLine() method says:

Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.

So, you need to call nextLine() inside while loop to iterate through the lines and reach end of file in order for control to come out of it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download