Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7650911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:22:24+00:00 2026-05-31T11:22:24+00:00

I’ve typed it exactly as shown in Introduction to Java Programming (Comprehensive, 6e). It’s

  • 0

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 image of the folder and input file
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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:22:26+00:00Added an answer on May 31, 2026 at 11:22 am

    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.

        $ java LotteryNumbers < lottery.txt
    

    … ‘cos your program expects the input to appear on the standard input stream.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.