patternjavaMinor
Check if all lotto numbers are covered in input
Viewed 0 times
allarenumbersinputcoveredchecklotto
Problem
Each ticket for the Pick-10 lotto has 10 unique numbers ranging from 1 to 99. Suppose you buy a lot of tickets and like to have them cover all numbers from 1 to 99. Write a program that reads the ticket numbers from a file and checks whether all numbers are covered. Assume the last number in the file is 0. Suppose the file contains the numbers:
Your program should display:
"The tickets cover all numbers"
Suppose the file contains the numbers
Your program should display:
"The tickets do not cover all numbers"
80 3 87 62 30 90 10 21 46 27
12 40 83 9 39 88 95 59 20 37
80 40 87 67 31 90 11 24 56 77
11 48 51 42 8 74 1 41 36 53
52 82 16 72 19 70 44 56 29 33
54 64 99 14 23 22 94 79 55 2
60 86 34 4 31 63 84 89 7 78
43 93 97 45 25 38 28 26 85 49
47 65 57 67 73 69 32 71 24 66
92 98 96 77 6 75 17 61 58 13
35 81 18 15 5 68 91 50 76
0Your program should display:
"The tickets cover all numbers"
Suppose the file contains the numbers
11 48 51 42 8 74 1 41 36 53
52 82 16 72 19 70 44 56 29 33
0Your program should display:
"The tickets do not cover all numbers"
import java.util.Scanner;
public class LottoCheck {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//Create an array of booleans
boolean[] neededNumbers = new boolean[99];
//Prompt input
System.out.println("Enter your ticket numbers: ");
int ticketNumber = input.nextInt();
while (ticketNumber != 0) {
for (int i = 1; i <= neededNumbers.length; i++) {
neededNumbers[i] = true;
}
ticketNumber = input.nextInt();
}
for (int i = 0; i < neededNumbers.length; i++) {
if (neededNumbers[i] == false) {
System.out.println("You do not have all the numbers.");
System.exit(0);
}
}
System.out.println("You have all the numbers.");
}
}Solution
First, this will not work with the input
This part is also erroneous:
Here, you input the values until the input equals
This loop is almost right, but you check the value an index
You can solve this problem in one of two ways - starting with
99. Arrays are 0-indexed, but when you create one, you need to specify the value you wish .length to have, which is 100.This part is also erroneous:
while (ticketNumber != 0) {
for (int i = 1; i <= neededNumbers.length; i++) {
neededNumbers[i] = true;
}
ticketNumber = input.nextInt();
}Here, you input the values until the input equals
0, but you loop from 1 through needeNumbers.length and set the values to true. First off, this will crash when you run it because the value neededNumbers[neededNumbers.length] does not exist. Additionally, your logic is wrong as you do not want all the values of neededNumbers to be set to true, just the input ones:while (ticketNumber != 0) {
neededNumbers[ticketNumber] = true;
ticketNumber = input.nextInt();
}This loop is almost right, but you check the value an index
0, which you did not set to true because 0 was the value to exit the loop:for (int i = 0; i < neededNumbers.length; i++) {
if (neededNumbers[i] == false) {
System.out.println("You do not have all the numbers.");
System.exit(0);
}
}You can solve this problem in one of two ways - starting with
i = 1, or changing your while loop to use neededNumbers[ticketNumber - 1] = true; and intializing it to new boolean[99], like you currently do. This will have value 1 be stored in the 0 index of the array, so all values will be set to true once all the numbers in the range of 1 - 99 have been entered.Code Snippets
while (ticketNumber != 0) {
for (int i = 1; i <= neededNumbers.length; i++) {
neededNumbers[i] = true;
}
ticketNumber = input.nextInt();
}while (ticketNumber != 0) {
neededNumbers[ticketNumber] = true;
ticketNumber = input.nextInt();
}for (int i = 0; i < neededNumbers.length; i++) {
if (neededNumbers[i] == false) {
System.out.println("You do not have all the numbers.");
System.exit(0);
}
}Context
StackExchange Code Review Q#85134, answer score: 5
Revisions (0)
No revisions yet.