Hi everyone. Hope everybody is having a fantastic day. I have a simple question and need your advice to guide me towards a solution. My question is, rather, I need help with. I'm doing the game hangman and I currently have a string that contains the secret word that is randomly chosen. I am using a mouse listener and I have a method that contains the letter of the button that was clicked,"a,b,c,d"etc. I'm trying to compare that letter(which is a string) to see if the letter clicked is a letter of the secret word. Any advice, help, example would be appreciated.

public void buttonClicked(String letter) {
[B]if(letter)[/B]<----Here is where I need advice
       {
       	System.out.println("Hello!");
       }
        
        
        // you write the check procedure here
        // the parameter 'letter' will contain the value of the correponding
        // clicked button
    }
    public class MyMouseAdapter extends MouseAdapter {
       private String letter;
        public MyMouseAdapter(String letter) {
            this.letter = letter;
        }
        public void mouseClicked(MouseEvent evt) {
            buttonClicked(this.letter);
        }
    
}

This is the part where I randomly choose the secret word

String[] words= {"hello","why","relationship","baby","ballistics"};
  char array[]=words[0].toCharArray();
  char array1[]=words[1].toCharArray();
  char array2[]=words[2].toCharArray();
  char array3[]=words[3].toCharArray();
  char array4[]=words[4].toCharArray();
String randomString=words[(int)(words.length*Math.random())];

THANKS A LOT!

Recommended Answers

All 4 Replies

I would convert letter to a char and then compare that char with your charArray word using

for (int i=0; i<array.size(); i++) {
  if (letter.charArray[0] == array[i]) {
    // the letter is at position i
  }
}

i am doing the same hangman could you give me some advice here is what i have

Futbol10, you already have a thread for this question. Please don't posts requests for help into other posters' threads (especially not old posts).

ok sorry

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.