Hey i was wondering if someone could have a look at this and help me figure out why the output is repeating it's self and how I would fix it. I have spent several hours trying to fix this. Thanks in advance.

/* Author : 
 * Date :02/10/10
 * Title: Hangman

import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
import java.awt.Color;
import java.awt.Font;

public class Hangman implements ActionListener {

    JButton button;
    JTextArea text = new JTextArea();
    JTextField guess;
    JLabel AlphaGuess = new JLabel();
    JTextField RandomWord;
    JLabel RandomWordDisplay = new JLabel();
    JLabel Result = new JLabel();
    String Wordcollection[] = new String[] {"plasma", "chicken", "mouse"}; //Array of words
    char LetterInput [] = new char [10];
    String Length = ""; 
    String Randomword;
    char LetterGen;

    public static void main(String[] args) 
        Hangman gui = new Hangman();
    public void go() 
        JFrame frame = new JFrame("Hangman"); //The programs name is Hangman
        JPanel panel = new JPanel();
        JLabel label = new JLabel("Guess a letter for the word : ");
        guess = new JTextField(20);
        button = new JButton("Solve!");
       //************ This section of code makes the title display in a different format *************  
        Font font = new Font("Verdana", Font.BOLD, 16);
        JLabel text = new JLabel("\n \n  H _ n g m _ n");
        //*********** This section of code displays the order of which things are displayed on my panel.       
        //************ This section of code gives the length and width of the program *****************



        //************ This section generates and hides a random word *********************************    
        Random Number = new Random();  //Random Number is produced  
        int Word = Number.nextInt(Wordcollection.length); //value is assigned to Word
        Randomword = Wordcollection[Word]; // will go to the certain point in the words array
        int WordLength = Randomword.length()+1; //Word length is is assigned
        	for(int i=1;i <WordLength; i++)
        			RandomWordDisplay.setText(Length = Length + '-'); //will display each letter as a - on the panel

    // ************** This method allocates what each button should do ********************************
    public void actionPerformed(ActionEvent event) 
    	if(event.getSource() == button)

    // ************ This method switches the _ with a letter if correct *******************************
    public void Process()
            String Guess = guess.getText();
              for (int i=0; i<Randomword.length(); i++)
                      if (Randomword.charAt(i) == Guess.charAt(0))
                    	  if  (LetterInput [i] != '-')
                       		  LetterInput [i] = Guess.charAt(0);
                    		  Result.setText("1. Congratualtions you have the correct letter " + Guess);
                    	  if  (LetterInput [i] == '-')
                    		   LetterInput [i] = '-';  
                    	 	   Result.setText("2. You have entered an incorrect letter " + Guess);
                      if (Guess.length()>1)
                    	  Result.setText("Please only enter one letter!");                
              for (int i=0; i<Randomword.length(); i++)  
            	  	Length = Length + Character.toString(LetterInput [i]);

Edited by Nick Evan: Real name removed

5 Years
Discussion Span
Last Post by SeasideP

The program atm currently generates a word from the choice

"plasma", "chicken", "mouse"


Just a quick glance at 160 shows you're rebuilding the string named "Length" every time. Is that correct?

...and at 95, you're displaying and adding simultaneously. Is that correct?


Just a quick glance at 160 shows you're rebuilding the string named "Length" every time. Is that correct?

...and at 95, you're displaying and adding simultaneously. Is that correct?

Yes the line at 160 i belive is my problem I don't wish to rebuild the string "Length" EVERYTIME so that it duplicates it self.

Line 95 has now been taken out

the code at 95 was oringally to replace the random word as " _ " for the number of char's in the word for example "_ _ _ _".

I then changed it to "-" then setup a char array so that I could store the input from the guess entered, That didn't work so then I tried to set up my if statments saying if the word is displayed with a "-" replace with the guess which was entered and if the word has a letter do not replace.....

Sorry if this seems confusing, this code has been doing my head in =[!

Edited by SeasideP: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.