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

  • SEARCH
  • Home
  • 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 9255769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:47:04+00:00 2026-06-18T11:47:04+00:00

I have this Hangman program that i am working on for school and i

  • 0

I have this Hangman program that i am working on for school and i cant get the number of wrong guesses to print correctly when the user guesses a wrong letter. Here is my code that i got so far, i would appreciate any tips.

import java.util.Scanner;

public class HangmanTest {
    public static void main(String[] args) {

        String[] wordBank = { "madelynn", "crystal", "mcbride", "daughter",
                "adorable", "beautiful", "andrew", "programming", "alyssa",
                "computers", "mcbreezy", "maddy", "happy", "vacation", "beach",
                "java", "benefical", "military", "veteran", "standale",
                "lions", "tigers", "redwings", "pistons", "michigan",
                "football", "baseball", "hockey", "basketball", "golf" };
        int minimum = 0;
        int maximum = wordBank.length - 1;
        String again;

        do {
            int choice = minimum + (int) (Math.random() * maximum);

            String word = wordBank[choice];

            // Converts the random word to asterix
            String userWord = "";
            for (int i = 0; i < word.length(); i++) {
                userWord += "*";
            }

            // Breaks into a bunch of characters
            char[] userWordCh = userWord.toCharArray();

            // Show the random word
            System.out.println("The word for you to guess is " + userWord);

            // instantiate a scanner object
            Scanner input = new Scanner(System.in);

            int size = word.length();
            int rightGuesses = 0;
            int wrongGuesses = 0;

            while (size != rightGuesses) {
                System.out.println("Enter a character: ");
                String response = input.next();
                char ch = response.charAt(0);

                char[] wordChars = word.toCharArray();

                for (int i = 0; i < word.length(); i++) {
                    if (wordChars[i] == ch) {
                        userWordCh[i] = ch;
                        ++rightGuesses;
                    } else {
                        ++wrongGuesses;
                    }
                } // end of for loop

                System.out.print("The word is: ");
                for (int j = 0; j < userWordCh.length; j++)
                    System.out.print(userWordCh[j]);

                System.out.println();
            } // end of while loop

            System.out.println("You had " + wrongGuesses + " wrong guesses.");

            System.out.println("Would you like to play again y/n: ");
            again = input.next();

        } while (again.equals("y"));

    }
}
  • 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-18T11:47:05+00:00Added an answer on June 18, 2026 at 11:47 am

    problem is in your for loop. You are iterating over each letter, and for every letter that doesn’t match yours, you mark it as an incorrect guess. It should only be marked incorrect if NONE of the letters are correct. Additionally it should only be marked right if you haven’t marked it already.

    import java.util.Scanner;
    import java.util.ArrayList;
    
    public class HangmanTest {
        public static void main(String[] args) {
    
            String[] wordBank = { "madelynn", "crystal", "mcbride", "daughter",
                    "adorable", "beautiful", "andrew", "programming", "alyssa",
                    "computers", "mcbreezy", "maddy", "happy", "vacation", "beach",
                    "java", "benefical", "military", "veteran", "standale",
                    "lions", "tigers", "redwings", "pistons", "michigan",
                    "football", "baseball", "hockey", "basketball", "golf" };
            int minimum = 0;
            int maximum = wordBank.length - 1;
            String again;
    
            do {
                int choice = minimum + (int) (Math.random() * maximum);
    
                String word = wordBank[choice];
    
                // Converts the random word to asterix
                String userWord = "";
                for (int i = 0; i < word.length(); i++) {
                    userWord += "*";
    
                }
    
                String guessedLetters="";
                // Breaks into a bunch of characters
                char[] userWordCh = userWord.toCharArray();
    
                // Show the random word
                System.out.println("The word for you to guess is " + userWord);
    
                // instantiate a scanner object
                Scanner input = new Scanner(System.in);
    
                int size = word.length();
                int rightGuesses = 0;
                int wrongGuesses = 0;
                boolean foundLetter;
                char[] wordChars = word.toCharArray();
    
                guessLoop:
                while (size != rightGuesses) {
                    System.out.println("Enter a character: ");
                    String response = input.next();
                    char ch = response.charAt(0);
    
    
                    foundLetter=false;
                    for (int i=0;i<guessedLetters.size();i++){
                        if (ch == guessedLetters.charAt(i)){
                             System.out.println("Already guessed that letter!");
                             continue guessLoop;
                        }
                    }
    
                    guessedLetters+=response;
                    for (int i = 0; i < word.length(); i++) {
                        if (wordChars[i] == ch) {
                               foundLetter=true;
                               userWordCh[i] = ch;
                               ++rightGuesses;                            
                        } 
                    } // end of for loop
                    if(!foundLetter)
                         ++wrongGuesses;
                    System.out.print("The word is: ");
                    for (int j = 0; j < userWordCh.length; j++)
                        System.out.print(userWordCh[j]);
    
                    System.out.println();
                } // end of while loop
    
                System.out.println("You had " + wrongGuesses + " wrong guesses.");
    
                System.out.println("Would you like to play again y/n: ");
                again = input.next();
    
            } while (again.equals("y"));
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have spent many hours working on this program that is supposed to play
Have this strange problems on some pages that rendering of umlauts (åäö) gets wrong
I have one question regarding this hangman program of mine.When I guess the a
I have this form: <%= form_tag posts_path, :method => :get, :class => search_nav do
I have a very specific question regarding my Hangman program. You will probably want
I am currently programming hangman for learning purposes. For that, I have a text
I have a hangman game where the user can choose one player or two
Have this print output from print_r($theobject); SimpleXMLElement Object ( [@attributes] => Array ( [label]
I need to have this file print to an array, not to screen.And yes,
I have this problem, and I can't figure it out whats is wrong. I

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.