I’ve typed it exactly as shown in Introduction to Java Programming (Comprehensive, 6e). It’s pertaining to reading integer input and comparing user input to the integers stored in a text file named “lottery.txt”

An external link of the image: https://i.stack.imgur.com/egPWq.jpg
Here’s my code:
import java.util.Scanner;
public class LotteryNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Defines and initializes an array with 100 double elements called isCovered.
boolean[] isCovered = new boolean[99];
// Prompts user for input and marks typed numbers as covered.
int number = input.nextInt();
while (number != 0) {
isCovered[number - 1] = true;
number = input.nextInt();
}
// Checks whether all numbers are covered.
boolean allCovered = true;
for (int i = 0; i < 99; i++)
if (!isCovered[i]) {
allCovered = false;
break;
}
// Outputs result.
if(allCovered) {
System.out.println("The tickets cover all numbers."); }
else {
System.out.println("The tickets do not cover all numbers."); }
}
}
I suspect the problem lies within the declaration of the array. Since lottery.txt does not have 100 integers, the elements from index 10 to 99 in the array are left blank. Could this be the problem?
Why does the program terminate without asking for user input?
Possible Solution:
After thinking for a while, I believe I understand the problem. The program terminates because it takes the 0 at the EOF when lottery.txt is feed in. Furthermore, the program displays all numbers not to be covered because the elements from 11 to 100 are blank. Is this right?
The program is written to keep reading numbers until a zero is returned by
nextInt(). But there is no zero in the input file, so the loop will just keep going to the end of the file … and then fail when it tries to read an integer at the EOF position.The solution is to use
Scanner.hasNextInt()to test whether you should end the loop.And, make sure that you redirect standard input from your input file; e.g.
… ‘cos your program expects the input to appear on the standard input stream.