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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:07:08+00:00 2026-05-28T18:07:08+00:00

Basically, it’s for a hangman game and I initialize a guessed character char array

  • 0

Basically, it’s for a hangman game and I initialize a guessed character char array of the same length of the word that’s to be guessed, but without putting anything in the array. After each guess, I want to add the guessed character to the array, and it will check whether or not the character has been guessed, but I’m having difficulty implementing this.

What I’ve tried is having this helper method in my Hangman class:

private boolean checkIfGuessed(char c) {
        boolean guessed = false;
        for (int i = 0; i < guessedChars.length; i++) {
            if (guessedChars[i] == c) {
                guessed = true;
                break;
            }
            else if (guessedChars[i] != c && guessedChars[i] == 0) {
                guessedChars[i] = c;
            }
        }
        for (int i = 0; i < guessedChars.length; i++) {
            System.out.print(guessedChars[i] + " ");
        }
        return guessed;
    }

But it’s not doing what I’d like, it simply fills the array with whatever letter is first guessed. (e.g. no matter where the game is, if the player guessed a first, and the array was of length 6, it would be aaaaaa)

I’m trying to do this by having a variable to flag if it’s been guessed (and returning that to another method) and it’s set to guessed if it loops through the array and finds the same character at some index that was initially guessed and will break out. Otherwise, if a certain index isn’t equal to the character guessed, and it’s equal to 0 (I know in numerical arrays, this signifies empty, not sure if in char arrays), meaning empty, it will write the guessed letter to the index the loop is at.

Problem being obviously it loops through and seems to label all the indices in the array to the first character guessed.

Is there a better way to do this/what am I doing wrong?

  • 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-28T18:07:09+00:00Added an answer on May 28, 2026 at 6:07 pm

    You add the character in a loop and don’t exit the loop after you add it. As it stands now, you’d need to scan the entire array (since it’s unsorted), indicate if it was in the array by setting a flag or storing an index if found, and add it to the array if it wasn’t found.

    Personally, I’d keep the guessed characters in a set.

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

Sidebar

Related Questions

Basically I'm trying to accomplish the same thing that mailto:bgates@microsoft.com does in Internet Explorer
Basically I’ve heard that certain conditions will cause .NET to blow past the finally
Basically from a database I am getting data that is formatted like this nameofproject101
Basically I have an iframe loaded that is accessed from the parent whenever it
Basically, I have a main page (parent page) with a link that opens up
Basically this function is meant to store the height value of the element that
basically I have a function that resizes elements accordingly with jquery triggering the function
Basically I have an IFRAME that contains a given page - I want to
Basically, I would like a page that consist of a slideshow that contains a
Basically I'm trying to improve on the Ghosts in a Pacman game I'm making.

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.