Hi, I have a quick question. I am making a hang man game for project (I am at university as a computer science major), I have completed most of the game but have run into one small issue which has been given me a big problem. Right now I am using an ArrayList in conjunction with nested loops and methods to get the game done. Again the game is finished, there us just one issue which I am having. One of the requirments is that when the user inputs a letter, and the letter that is guessed mathches a letter(s) from the word than the console is supposed to print out that the user has gotten the letter right. However, the console has to print out the letter in the order it is in the original word.
ex. Word is: "nice"

    use guesses: i


what the console is supposed to output: "[ ,i, , ]" - or something similar
Right now I have gotten the program the to print out the users right guesses but it only prints it out in the order the user guessed the letter, not the order the letter is in the actual word. I appreciate all and any help. Please find the code that I have already written below.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import javax.swing.JOptionPane;

public class activity1 {
    public static void main(String[] args) {
        //Welcome message
        JOptionPane.showMessageDialog(null, "Welcome to the game of hang man, you must"
                + "\nsuccessivley guess the word within 6 guesses or the guy gets hanged."
                + "\nPress 'OK' to continue");
        String copy;
        //This do loop iterates the loop as long as the user wants to play
        do{
            //declaration of the array lists
        ArrayList <String> cont = new ArrayList <String>();
        cont.add("h");
        cont.add("e");
        cont.add("e");
        cont.add("p");
        //this array list is responsible of keeping track of the letters that the user has guessed right
        ArrayList <String> lett = new ArrayList <String>();
        String dec;
        final int word = cont.size();
        final String [] ret = new String [word];
        cont.toArray(ret);
        System.out.println(word);
        int count = 0;
        String alpha = "i";
        System.out.println("The word has " + cont.size() + " letters.");
        int inccount = 0;
        String hat;
        String listString = "";
        Scanner input = new Scanner(System.in);
        //This do loops iterated until the guy is hanged
        do{
            dec = input.nextLine();
            test(cont, dec, lett);
            //Test = true
            if(test(cont, dec, lett)){
              System.out.println("Good guess");
              alpha.equals(dec);
              //this method is responsible for keep track of how many letters of the right letter guessed are in the word
              int cheese = multiple(cont, dec);
              if(cheese > 1){
                  //this for loop adds the letter to the 2nd arraylist and removes from the first
                  for(int c = 0; c < cheese; c++){
                      lett.add(dec);
                      cont.remove(dec);
                  }
              }else{
                  lett.add(dec);
                  cont.remove(dec);
              }
              ///////////////////////////////////////////////////////////////////////////////
             //THIS IS THE CODE THAT I HAVE BEEN TRYING TO SOLVE THE PROBLEM DESCRIBED IN THE POST
              String [] check = new String[word];
              lett.toArray(check);
              for(int c = 0; c < ret.length; c++){
                  if (ret[c].equals(check[c]))
                  {
                      System.out.println(check[c]);
                  }
              }
              for(String s: check){
                  System.out.print(s);
              }
              System.out.println(check.length);
              ///////////////////////////////////////////////////////////////////////////////
              System.out.println("There are " + cheese + " " +  dec + "'s in the word");
              System.out.print("So far you have guessed the following letters ");
              if(lett.size() <= 0)
                  System.out.println("N/A");
              else
                  System.out.println(lett);
              if(inccount == 0)
              gallow();
              else if (inccount > 0){
                  //Switch statement which displays different parts of the guy depending on the number of wrong guesses
                  switch(inccount){
                    case 1:
                        head();
                        break;
                    case 2:
                        body();
                        break;
                    case 3:
                        rightLeg();
                        break;
                    case 4:
                        leftLeg();
                        break;
                    case 5:
                        rightArm();
                        break;
                }
              }
            //Test = false
            }else if(!test(cont, dec, lett)){
            if(lett.contains(dec)){
                System.out.println("There are no more " + dec + "'s");
            }else{
              System.out.println("Bad guess, there are no " + dec + "'s in the word");
              System.out.print("So far you have guessed the following letters ");
              if(lett.size() <= 0)
                  System.out.println("N/A");
              else
                  System.out.println(lett);
              ++inccount;
              //switch statement similar to the one in the test= true section
              switch(inccount){
                case 1:
                    head();
                    break;
                case 2:
                    body();
                    break;
                case 3:
                    rightLeg();
                    break;
                case 4:
                    leftLeg();
                    break;
                case 5:
                    rightArm();
                    break;
                case 6:
                    leftArm();
                    break;
              }
            }
            }
        }while(cont.size() >0 && inccount < 6);
       if(cont.size() > 0){
            System.out.println("You have used up all your guesses, you lose.");
            copy= JOptionPane.showInputDialog("If you would like to start a new game please type the letter 'y'");
       }
       else
           System.out.println("You won, the word was heep");
       copy= JOptionPane.showInputDialog("If you would like to start a new game please type the letter 'y'");
        System.out.println("you won");
        }while(copy.equals("y") || copy.equals("Y"));
    } 
    // this loop is responsible for deciding whether or not a users guess is consistent with the letters in the word
    public static boolean test(ArrayList <String> xr , String f, ArrayList <String> craps){

        for(int x = 0; x < xr.size();++x){
          if(f.equals(xr.get(x))){
              return true;
          }
      }
      return false;

}
    //these methods are responsible for drawing the person
    public static void gallow(){
        System.out.println("--------|\n|\n|\n|\n|\n-------------");                           
    }
    public static void head(){
        System.out.println("--------|\n" + "|       O" + "\n|\n|\n|\n-------------");   
    }
    public static void body(){
        System.out.println("--------|\n" + "|       O" + "\n|       |" + "\n|       |" + "\n|" + "\n-------------");
    }
    public static void rightLeg(){
        System.out.println("--------|\n" + "|       O" + "\n|       |" + "\n|       |" + "\n|        |" + "\n-------------");
    }
    public static void leftLeg(){
        System.out.println("--------|\n" + "|       O" + "\n|       |" + "\n|       |" + "\n|      | |" + "\n-------------");
    }
    public static void rightArm(){
        System.out.println("--------|\n" + "|       O" + "\n|       |>>>" + "\n|       |" + "\n|      | |" + "\n-------------");
    }
    public static void leftArm(){
        System.out.println("--------|\n" + "|       O" + "\n|    <<<|>>>" + "\n|       |" + "\n|      | |" + "\n-------------");
    }
    //this method is responsible keeping track of letters which repeat in a word
    public static int multiple(ArrayList <String> pit, String ent){
        int count = 0;
        String prime = "f";
        int results;
        for(int cat = 0; cat < pit.size(); cat++){
            if(pit.get(cat).equals(ent)){
                count++;
            }
    }
        return count;
}
    //This method will take the word in a txt file and then converts to an array list
    //DANIWEB USERS, PLEASE DISREGARD THIS METHOD, IT HAS NO VALUE IN THE PROGRAM AT THIS POINT
    public static ArrayList <String> atos(String gad) throws FileNotFoundException{
        File list = new File("words.txt");
        Scanner read = new Scanner(list);
        String go = read.nextLine();
        System.out.println(go);
        char[] cArray = go.toCharArray();
        ArrayList <String> strin = new ArrayList <String>(cArray.length);
        for(char c : cArray){
            strin.add(String.valueOf(c));
        }
        return strin;
    }

    public static void order(String[] h){

        for(String hit: h){
            System.out.print(hit + " ");
        }
        System.out.print("\n");
    }

}

Masybe you could use a boolean array or list, same size as the secret word, that shows which letters have been found. As the user guesses a letter correctly you set the corresponding entry to true. You can then use that array to control the printing of the secret word - only showing letters where the corresponding boolean is true.

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.