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 8462585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:12:18+00:00 2026-06-10T14:12:18+00:00

I’m making a console-based game of black jack that prompts the user asking him/her

  • 0

I’m making a console-based game of black jack that prompts the user asking him/her if he/she wants to: ‘h’ for hit, ‘s’ for stay, or ‘q’ for quit. I’m using the Scanner class to receive input from the user in a while loop. The code works the first time it prompts the user and receives input, but it never works the second time. After the second prompt comes up, no matter what the user types, the program just waits and does nothing even though it’s still running. I’ve been trying to get this to work for hours and have read the Java Docs, many SO questions, etc. Here’s the relevant code:

public void gameloop() {
    while (thedeck.cards.size() >= 1) {
        prompt();
    }
}

public void prompt() {
    String command = "";
    Boolean invalid = true;
    System.out.println("Enter a command - h for hit, s for stay, q for quit: ");

    Scanner scanner = new Scanner(System.in);

    while (invalid) {
        if (scanner.hasNext()) {
            command = scanner.next();
            if (command.trim().equals("h")) {
                deal();
                invalid = false;
            } else if (command.trim().equals("s")) {
                dealerturn();
                invalid = false;
            } else if (command.trim().equals("q")) {
                invalid = false;
                System.exit(0);
            } else {
                System.out.println("Invalid input");
                scanner.next();
            }
        }
    }
    scanner.close();
}

Here’s what the code outputs:

Dealer has shuffled the deck.
Dealer deals the cards.
Player's hand:
Three of Clubs: 3
Five of Clubs: 5
Enter a command - h for hit, s for stay, q for quit: 
h
Dealer deals you a card:
Player's hand:
Three of Clubs: 3
Five of Clubs: 5
Queen of Hearts: 10
Enter a command - h for hit, s for stay, q for quit: 
h (Program just stops here, you can keep entering characters, 
but it does nothing even though the code is still running)

Any idea as to what’s going wrong would be greatly appreciated. I also realize the while loop is a little ugly, but I just want to get this program in working condition before I start to revamp any code.

  • 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-06-10T14:12:20+00:00Added an answer on June 10, 2026 at 2:12 pm

    From the documentation for Scanner.close:

    When a Scanner is closed, it will close its input source if the source implements the Closeable interface.

    Here you close your scanner, and this causes System.In to be closed, which means you can’t read any more input:

    scanner.close();
    

    It is better to open the scanner once and reuse it. Close it only when are sure you have finished reading all input, or are closing your application.

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

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this

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.