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!

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

Attachments
/**
 * @(#)Hangman.java
 *
 * Hangman Applet application
 *
 * @author 
 * @version 1.00 2008/5/27
 */
 
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.JOptionPane;

public class MyHangman extends Applet  {
 
 
 private boolean usd[] = new boolean[26];

 private String guess;

 private int numguesses = 0;

 private boolean finished = false;

 private boolean won = false;

 private Button b[];
 
 char secretWord[];//holds letters in word
 
 char word[];//holds letters guessed in word
 
 char wrongWord[];

	
	public void init() {
	
		
		int a;
		StringBuffer buff = new StringBuffer();
		
		
		setLayout ( new FlowLayout());
		b = new Button[26];
		
		for(a = 0; a < 26; a++)
		{
			buff = new StringBuffer();
			buff.append((char)(a+65));
			b[a] = new Button(buff.toString());
			//b[a].addActionListener(this);}
			
			add(b[a]);//adds all of the buttons
			//guess = getParameter("wrd").toUpperCase();
			
			
		}
		
	}
	
	public void paint(Graphics g)
	{
		setBackground(Color.orange);
		
		g.fillRect(450, 700, 200, 20);
		g.fillRect(450,550,10,100);
		g.fillRect(450,300,20,400);
		g.fillRect(450,300,150,20);
		g.setColor(Color.blue);
		g.fillRect(525,320,25,75);//draws the gallows
		g.setColor(Color.white);
		/*g.drawRect(535,445,10,100);
		g.drawOval(515,395,50,50);
		g.drawRect(545,475,30,5);
		g.drawRect(505,475,30,5);
		g.drawOval(525,410,5,5);
		g.drawOval(540,410,5,5);
		g.drawRect(530,420,6,6);
		g.setColor(Color.black);
		g.drawRect(530,440,18,1);*/
	
		
		if ( numguesses >= 1 )
			g.setColor(Color.white);
			g.fillOval(515,395,50,50);
			
			g.setColor(Color.blue);
			g.fillOval(525,410,5,5);
			g.fillOval(540,410,5,5);
			g.drawRect(530,437,18,1);
			g.drawRect(530,420,6,6);
			
			
			g.setColor(Color.red);
		
		if ( numguesses >= 2)
			g.fillRect(535,445,10,100);
			
		if ( numguesses >= 3)
			g.fillRect(545,475,30,5);
			
		if ( numguesses >= 4)
			g.fillRect(505,475,30,5);
			
		if ( numguesses >= 5)
			g.drawLine(535,540,500,570);
			
		if( numguesses >= 6)
			g.drawLine(580,570,545,540);		
			
			StringBuffer wrong = new StringBuffer();
			
			  for (int l = 0; l<=25; l++) {
              if (usd[l]) wrong.append((char)(l+65));
              else wrong.append(".");
	
			}
			
			g.setColor(Color.green);//Setting up the incorrect guessed letters
			
			Font f = new Font("Palatino",Font.BOLD+Font.ITALIC,65);
			g.setFont(f);
			g.drawString(wrong.toString(),300,850);//end of writing the wrong letters
		
			StringBuffer right = new StringBuffer();
									
			Font ff = new Font("Palatino",Font.BOLD,85);
			g.setColor(Color.black);
			
			g.setFont(ff);
			
				for (int ii = 0; ii < guess.length(); ii++)
				if(usd[(int)guess.charAt(ii)-65])
					right.append(guess.charAt(ii));//Marks down the right answers
					else
					right.append(".");	//If the same letter is used twice it won't be put down
										//or counted
		
			g.drawString(right.toString(),600,400);	
				
				
			if(numguesses >= 6)
			{
				JOptionPane.showMessageDialog(null, " Wow you just got your buddy hung nice job");
				finished = true;
			}
  
  			if(won)
  			{
  				JOptionPane.showMessageDialog(null, " Phew your buddy lives another day");
  			}




    }




			
			
		
		
			
			
	String wordList[] = {
		"BEAU",
		"SHERLOCK",
		"ROBIN",
		"LUKE",
		"AMY",
		"JASON",
		"AUGUSTIN",
		"FAITH",
		"BRANDON",
		"JENNIFER",
		"JAVA",
		"GREENE",
		"MORTON",
		"BLODGETT",
		"HOLMES",
		"PAPWORTH",
		"PETERSEN",
		"CHAMBERS",
		"HARRAL",
		"CAMPOS",};				
}	//end of main

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

This article has been dead for over six months. Start a new discussion instead.