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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:59:15+00:00 2026-06-10T21:59:15+00:00

So for a class we are having to make a hangman game that can

  • 0

So for a class we are having to make a hangman game that can take a user input for a word and then have another person solve for it. It has to be able to recognize multiple repeating letters in the word or I would be done. Below is my code, it works great until I remove the break statement in my checkformatch method so that it goes past the initial finding of a letter. With the break in there it never finds the second third etc repeated letters, without it, it returns that each letter that is not the letter searched is a miss and reduces my life count. What I’m needing is some hints on how to search my array for the letter that is inputted as a guess and return their index positions in the array without it thinking each character in the array that is not the one guessed is a wrong input. Thank you in advance.

package hangman;

import java.util.Scanner;

class Game {
int livesRemaining;
String letterGuessed;
String wordInput;
  char[] hiddenWord;
  char[] aOfWord ;

Scanner input = new Scanner(System.in);
boolean isFound;
int a;


public Game()
{
    this.setLives(8);
    //this.output();

    System.out.println("Player 1 please enter the word to be searched: ");
    wordInput = input.nextLine();

    aOfWord = wordInput.toCharArray();

    hiddenWord = new char[aOfWord.length];

    for(int j = 0; j < hiddenWord.length; j++)
        hiddenWord[j] = '*';

    this.output();

    while(livesRemaining > 0)
    {
        System.out.println("Please choose a letter: ");
        letterGuessed = input.nextLine();

        this.checkForMatch(letterGuessed);
        if(isFound == true)
    {
        hiddenWord[a] = letterGuessed.charAt(0);
    }
    else
    {
        System.out.println("Is not found!");
        this.reduceLives();
    }
    this.output();


  }

}

public void setLives(int a)
{
    this.livesRemaining = a;
}

public void reduceLives()
{
    livesRemaining = livesRemaining -1;
    System.out.println("Lives remaining: " + this.getLives());

}

public int getLives()
{
    return livesRemaining;
}

public void output()
{
    System.out.println("Lives remaining: " + this.getLives());
    System.out.println("Word found so far ");

    for(int i = 0; i < hiddenWord.length; i++)
    {
        System.out.print(hiddenWord[i] + "\n");
    }

}

public void checkForMatch(String l)
{

    for(int i = 0; i < aOfWord.length; i++)
        {
            //System.out.println("Comparing " + l.charAt(0) + " To " + aOfWord[i]);
            if(l.charAt(0) == aOfWord[i])   
            { 
               isFound = true;
               a = i;
               break;
            }
            else
            {
                isFound = false;  
            } 
        }

}

}
  • 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-10T21:59:16+00:00Added an answer on June 10, 2026 at 9:59 pm

    Your algorithm seems fine. However, it will only get you the last matching character because a will be rewritten whenever he finds a matching character.
    I think a really simple solution would be to do this in your checkForMatch method:

    if(l.charAt(0) == aOfWord[i])   
    { 
        isFound = true;
        hiddenWord[i] = l.charAt(0);
    }
    

    and also this in your game method…

    if(!isFound)
    {
        System.out.println("Is not found!");
        this.reduceLives();
    }
    

    You don’t have to use this. by the way. That is only necessary in certain cases. Take a look at this.

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

Sidebar

Related Questions

Someone ask me that do we make an object of class having private constructor
I have a List of objects that extend another class: List<? extends Fruit> arguments;
I am using Hibernate 4.1 and Spring 3.1. Consider the following User class having
Veterans please forgive me for asking silly question. I understand that a class having
I am having following class hierarchy - //Abstract Class1 in library1 (Can't modify this)
Having such class can I instatiate it in place? abstract class Mammal { public
I have a form in spring mvc 2 having command class Class1 I want
Imagine I have a game with a base Entity class, with an init method
I have a class member myMember that is a myType pointer. I want to
I need to make a class Expr having public interface like this: class Expr{

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.