0

I'm still pretty new to Java so bear with me. I'm making a simple hangman game that takes input from the user. I am trying to append the guessed letter to the knownLetters array but I get a type mismatch. I tried .concat() and got the same type error. Here is where I am now. Any ideas or documentation resources (that a novice can read) would be helpful. Thanks!

Edit: Thanks for the comments, everyone! These are very helpful.

 public static boolean updateWithGuess(char[] knownLetters,
                                      char guessedLetter,
                                      String word) {

    System.out.println(Arrays.toString(knownLetters));

    int i = 0;
    while(word.indexOf(i, guessedLetter) != -1) {
      i = word.indexOf(i, guessedLetter) + 1;
      knownLetters += guessedLetter;
7
  • You can't precisely append to an array. Arrays are fixed-length. You can create a new bigger array if that helps. Commented Oct 25, 2020 at 22:41
  • 1
    I would recommend using list, where you can remove and append items freely Commented Oct 25, 2020 at 22:42
  • 1
    Rather than using Array or List use StringBuilder and there you can append all the guessed characters. And when ever you want to check whether a character is present in the StringBuilder or not, just convert StringBuilder to String and use contains() function. Commented Oct 25, 2020 at 22:54
  • I can't comment as I'm a new user myself so I'm writing here. I believe += wouldn't even compile. This is not the way to append to an Array (apart from the fact that Arrays are fixed size as already mentioned in comments) Commented Oct 25, 2020 at 22:55
  • @SwapnilPadaya StringBuilder already has charAt or indexOf, both of which are much more efficient than creating unnecessary strings. Commented Oct 25, 2020 at 23:05

2 Answers 2

1

Using arrays in that case is not the best choice because arrays have a fixed size and you cannot dynamically add and remove items, maybe rather choose lists.

If you need a simple introduction, here you are:
https://www.geeksforgeeks.org/list-interface-java-examples/

Sign up to request clarification or add additional context in comments.

4 Comments

list take space of o(n) where as this can be done in constant space. Even tho what you suggested is True.
Surprised no one mentioned sets yet!
@OmarAbdelBari As you can read here (codejava.net/java-core/collections/…) sets are unordered (which is not very useful for hangman) and does not support duplicate items (which is also not very useful if you want to store words).
But why can't one manage a set for storing guessed letters and one for required letters? The actual word can be stored separate field. Nothing wrong with that. It's also common to do in games especially when there's a large amount of objects these kinds of redundancies can have performance benefits. Hangman is too simple to see those benefits but it can help with prevent having to handle a whole stack of edge cases.
0

Others have mentioned StringBuilder and List which are a step in the right direction as they dynamic (in number of elements). However since this question is only concerned with keeping track of each letter that was guessed a set makes the most sense. One common example is HashSet. Sets do not allow duplicates. However sets will allow both the upper and lower case versions of a character to be present in the set (since they have different values). So you could normalize all the guesses and known letters to either uppercase or lowercase.

3 Comments

But how do you then want to store a word like "Cheese" because there would then be a duplicate char.
Just to add using maps is a better option. rather than sets. characters are the key and the number of occurrences is the value and yet it's in constant time in average cases.
This is for only checking has the letter been already used? (what the snipping of code is dealing with). It's quite common to keep redundant data in games especially when there is a large scale of objects. You make use of the benefits of different data structures. For hangman performance isn't too important but it will likely lead to less logical errors and complicated conditional statements. Map would not be useful here as we only care about whether a letter has been guessed or not (quantity has no benefit).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.