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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:10:38+00:00 2026-05-28T22:10:38+00:00

For a class project, we have to create a hangman game in Java (we’re

  • 0

For a class project, we have to create a hangman game in Java (we’re starting off with object oriented programming, so this is getting us used to it) and I have the class file then the main file.

Everything’s working great, but I want one of my methods in the class file to tell the player if they’ve already guessed a particular character.

Basically, the method uses a for loop to add the guessed characters into a character array, and every time the player guesses it checks to see if that character is there (if it is, it breaks out of the loop) and if not, and the index value is 0 (this means it’s unwritten, right?) it will write the character that was guessed to that index value.

The only other thing that I don’t think is self explanatory in the code, is that if the player has yet to make any guesses, it makes the first value the guess, so it has something to start with in the array.

Any help would be appreciated, and if anyone has any input on how to improve my code or whatever, I’d be happy to hear that as well. Thanks so much. 🙂

public void makeGuess(char c) { 
    boolean alreadyGuessed = false, anyMatches = false;
    matches = 0;
    guesses++;
    if (guesses == 1) {
        guessedChars[0] = c;
    }
    for (int i = 0; i < guessedChars.length; i++) { //it goes through it and will see that it was already guessed
        if (guessedChars[i] == c) {
            alreadyGuessed = true;
            break;
        }
        else if (guessedChars[i] != c && guessedChars[i] == 0) {
            guessedChars[i] = c;
        }
    }

    if (alreadyGuessed == false) {
        for (int i = 0; i < word.length; i++) {
            if (word[i] == c) {
                anyMatches = true;
                matches++;
                disguisedWord[i] = c;
            }
        }
    }       
    if (anyMatches == true) {
        System.out.println("You guessed correctly!");
        System.out.println("There were " + matches + " matches.");
    }
    else if (alreadyGuessed == true) {
        System.out.println("You already guessed this letter, derp.");
    }
    else {
        System.out.println("Sorry, that character is not in the word to be guessed.");
        wrongGuesses++;
    }
//      for (int i = 0; i < guessedChars.length; i++) {
//          System.out.print(guessedChars[i] + " "); 
//      }       
    }

MAIN METHOD:

import java.util.Scanner;
class HangmanDemo {
    public static void main(String[] args) {
        //Object initialization
        Scanner keyboard = new Scanner(System.in);
        Hangman word = new Hangman("jordan");

        //Variable declaration
        char letterGuess;
        int limit;

        //Interact with user
        System.out.print("What would you like the limit of guesses to be: ");
        limit = keyboard.nextInt();

        //BEGIN ZE GAME
        while (word.isFound() == false && word.getWrongGuessCount() <= limit) {
            System.out.println();
            System.out.print("Letter guess: ");
            letterGuess = keyboard.next().charAt(0);
            word.makeGuess(letterGuess);
            System.out.println(word.getDisguisedWord());
        }
        if (word.getGuessCount() == limit) {
            System.out.println("\nSorry, too many guesses.");
        }
        else {
            System.out.println("\nCongratulations! You succesfully solved the word!");
        }
    }
}
  • 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-28T22:10:39+00:00Added an answer on May 28, 2026 at 10:10 pm

    So, check this out:

    First time you call the method, both alreadyGuessed and and anyMatches will stay false… and that’s obviously not good.

    Second: OOP is NOT procedural P. More concretely: a method is not supposed to do as many things as you do. A method should have a lot of explaining to do it if plans to be larger than 10 lines. That’s so full of language constructs you can’t go over it fast. Split your code… less local variables, more methods.

    Hope that helps.

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

Sidebar

Related Questions

So for my programming class we have had a project to create a virtual
I have a class library project which contains a java script file libscript.js which
In my eclipse project i have ip address string variable in many java class
I have object Project in my entity. I create project by: public static void
i have created a simple public ref class in the vc++ project, which is
i already have a project with TreeNode class which creates a hierachy of nodes
I have a Project model similar to: class Project(models.Model): ... lead_analyst=models.ForeignKey(User, related_name='lead_analyst') ... I
I have a class named project with the following data members. class Project {
I am using C# and developing a winform application. I have a project class
I have a Class Library project and MVC project in the one solution. My

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.