1

The code is not yet complete but I have had some issues. This is my current code:

public class Engine
{
    public static void main(String[] args)
    {
        String word = JOptionPane.showInputDialog("Player 1! Please enter a word.");
        JOptionPane.showMessageDialog(null, "Switch to Player 2. It is time to play hangman!");
        char[] letters = word.toCharArray();
        String[] underscores = new String[word.length()];
        for(int i=0; i<=word.length()-1; i++)
        {
          underscores[i]="_ ";
        }
        StringBuilder builder = new StringBuilder();
        for (int i=0; i<word.length(); i++)
        {
            builder.append(underscores[i]);
        }
        int mistakes = 0;
        while(mistakes!=7)
        {
            String answer = JOptionPane.showInputDialog(null, "Please guess a letter.\n" + "Mistakes = " + mistakes + " of 7!\n\n" + builder.toString() + "\n\n");
            char guess = answer.charAt(0);
            for(int i=0; i<=word.length()-1; i++)
            {
                if(guess==letters[i])
                {
                    underscores[i] = answer;
                    for (int x=0; x<word.length(); x++)
                    {
                        builder.append(underscores[x]);
                    }
                }
            }
        }
    }
}

The problem I am having is with the StringBuilder part and .append()

For example, if the word is "hat". A window will appear with three underscores, like so:


If I were to guess "h", then it would change to:

_ _ _ h _ _

INSTEAD OF

h _ _

Furthermore, if I were to guess "t" next, it would change to:

_ _ _ h _ _ h _ t

It adds the changed elements of the array to the already existing array. I am pretty sure it is caused by the append method, but I have no knowledge of any alternatives.

3
  • you are not supposed to append the string. Try replacing the character in the string..
    – Damodaran
    Nov 5, 2013 at 4:43
  • I'm not entirely sure what you mean. Isn't that what this code does? underscores[i] = answer; I used string builder because with JOptionPane, simply printing the array put each underscore on a different line.
    – user2954970
    Nov 5, 2013 at 4:47
  • Check answer by Black Panther
    – Damodaran
    Nov 5, 2013 at 4:49

2 Answers 2

0

the append method of the StringBuilder adds the new text to the existing text. that is why you are getting _ _ h _ _h_ and stuffs. if you are using StringBuilder for the sake of avoiding creation of new String Objects, then before appending the text clear the builder's existing text using delete() method and then append it.

like

builder.delete(0,builder.length()-1);
for(int x=0;x<words.length();x++) {
 builder.append(underscores[x]);
}

EDIT: your understanding is correct, it is possible to construct this game without the use of a StringBuilder also check the code below.

import java.util.Scanner;

class Game {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    String word = null;
    char[] letters;
    char[] underScores;
    char guess;
    int found=0;
    int mistake=0;
    boolean present = false;

    System.out.println("Enter the word");
    word = s.nextLine();

    letters = word.toCharArray();
    underScores = new char[letters.length];
    for(int i = 0; i<letters.length;i++) {
      underScores[i]='_';
    }

    while(true) {
      System.out.println("The word is");
      for(int i = 0; i<letters.length;i++) {
        System.out.print(underScores[i]);
      }

      System.out.println("\nEnter a letter");
      guess= s.nextLine().charAt(0);

      for(int i =0; i<letters.length;i++) {
        if(guess==letters[i]) {
          underScores[i]=guess;
          found+=1;
          present=true;
          break;
        }
      }

      if(!present) {
        System.out.println("the letter is not present");
        mistake+=1;
      }

      if(mistake >=7) {
        System.out.println("too many mistakes!! sorry");
        break;
      }

      if(found==letters.length){
        System.out.println(word);
        System.out.println("you have found the word!!");
        break;
      }
    } //ending while
  } //ending main
} //ending Game
2
  • IT WORKED! Alright. I want to understand how you accomplished this. Is my understanding correct? First, you used the delete method to completely erase the string saved from the old array. Second, you made a for loop that ran as many times as the length of the word. Third, the for loop contains code that calls the append method on builder once more and puts the elements in the underscores array into a string. It solves the problem because the old string has been deleted.
    – user2954970
    Nov 5, 2013 at 5:08
  • yes that is right.. this same thing can be accomplished without the use of StringBuilder. i have edited my post to give you the complete working code. try to go through it. Nov 5, 2013 at 5:16
0

Try the replace() and delete() methods of StringBuilder, or simply create a new StringBuilder for each guess.

2
  • I have never used StringBuilder before. How exactly would I use those? Would I be trying to delete and/or replace the elements in the array rather than have them added on to the end?
    – user2954970
    Nov 5, 2013 at 4:51
  • Yes, if you chose to go that route. Please read the documentation on StringBuilder here Nov 5, 2013 at 5:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.