hey people.i am new to java and i am currently constructing a hangman game. the game asks for the user to enter a word(3 or more characters long). then it prompts the user to enter a single character or the complete word in a message input dialog which contains the word lenght replaced by ("_") .. if the user enters a single character then the dashes are replaced by the character the user enters.if the user enters the right word then a message box appears(correct). i am asking for help at the point where the user is prompt to enter a single character or a complete word.how am i going to reappear the inputdialog with the dashes replaced?how is going to check character by character of the hidden word?thx

import javax.swing.*;

public class StringGame {
    public String word;
    /**
     * Creates a new <code>StringGame</code> instance.
     *
     */
    public StringGame() {
    }



public static void main(String [] args){
        StringGameFrame frame = new StringGameFrame();
        frame.setVisible(true);
    
    StringGame game;
    game=new StringGame();
/** tot is the total amount of characters of a word typed of the user **/ 
	String tot;
	tot="";
    String tot2;
    tot2="";
    char cha;
  
/** the program asks from user to enter a word*/
    String word=JOptionPane.showInputDialog(null,"Please enter a word.");

    String step1;
/** check about word length.if it is equal or less than 2, user is prompted to re-enter a valid word*/
    if ((word.length()<=2)){
    do{
    step1= JOptionPane.showInputDialog(null,"Please enter a word with 3 or more characters.");
    }while((step1.length()<=2));
    word=step1;
    }

for (int i=1; i<=word.length() ; i++){
            tot= tot + "_ ";
    }
   String step2=JOptionPane.showInputDialog(null,tot + "\nPlease enter a single character, or a word as your final guess.");
   if (step2.length()<=1){
// !!!!!! FAULTY PART !!!!! 
    for (int i=0;i<=word.length(); i++){
       
       cha=word.charAt[i];
   
       if (cha.equals(step2)) {
           tot2= tot2 + step2;
       } else{
           tot2=tot2 + "_ ";
     }
    }
      
   
       
   } else {
    if (step2.equals(word)){
              JOptionPane.showMessageDialog(null,"You guessed correctly");
          
          } else {
             JOptionPane.showMessageDialog(null,"You guessed incorrectly");
        }
      } 
    }

}

Please wrap your code in CODE tags

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.