Bvare Bvare - 1 month ago 13
Java Question

Trying to Generate a String as a Hint for the Solution to a Word Solve

I am trying to generate a String as a hint for the solution to a world solve.

This is what I have for generating the hint, but I am unsure of how to correct these errors. If the guess has the correct character guessed in the right place, the hint displays that character. If it has the letter in the word, it displays a "+" in the respective position. If the letter isn't in the word, a "*" gets returned.

For instance, if the solution to the puzzle is "HARPS", and the guess is "HELLO", the hint will be "H****". Likewise if the guess is "HEART", the hint will be "H*++*".

Also, wordLength is generated from another method that gives the amount of characters in the solution.

public String getHint(String theGuess) {
for (int index = 0; index < wordLength; index++) {
if **(theGuess.charAt(index)** = solution.charAt(index)) {
hint.**setCharAt**(index, theGuess.charAt(index));
} else if **(theGuess.charAt(index)** = solution.indexOf(solution)) {
**hint.setCharAt**(index, "+");
} else {
**hint.setCharAt**(index, "*");
}
}
return hint;
}


Errors are double starred.

For (theGuess.charAt(index) Eclipse is showing the following error message:


The left-hand side of an assignment must be a variable.


For hint.setCharAt, it tells me:


The method setCharAt(int, String) is undefined for the type String.

Answer

There are numerous problems in your code that need to be fixed:

  1. = is used when you want to assign a new value to a variable. You want to use == when comparing two values.
  2. setCharAt() is a method for StringBuilder, not String. This simplest solution is to just concatinate your new charater to the String using +=.
    If you want to use StringBuilder, the following parts need to be fixed:
    • The second parameter for setCharAt() should be a character, not a string. You need to change the double quotes around "*" and "+" to single quotes like '*'
    • setCharAt() tries to replace a character at a specifc index. This will throw an error is the StringBuilder is shorter than the index position you are trying to replace. You can solve this by right away setting your StringBuilder to a string that is the correct length like
      hint = new StringBuilder("*****").
      Since you are always adding the the end of the builder though, you should really just use append() instead of setCharAt() and you won't need to worry about this index position problem.
  3. (theGuess.charAt(index) == solution.indexOf(solution) does not search the entire solution string to see if it contains the current character. Instead, you can use indexOf() to check if the string contains the character. This link might help: How can I check if a single character appears in a string?

Here is a complete program with the code working:

public class HelloWorld
{
    public static void main(String[] args)
    {
        OtherClass myObject = new OtherClass();
        System.out.print(myObject.getHint("HEART"));
    }
}

Option 1 - Add to the String using +=:

public class OtherClass
{
    private String solution = "HARPS";
    private int wordLength = 5;

    public String getHint(String theGuess) {
        String hint = "";

        for (int index = 0; index < wordLength; index++) {
            if (theGuess.charAt(index) == solution.charAt(index)) {
                hint += theGuess.charAt(index);
            } else if (solution.indexOf(theGuess.charAt(index)) > 0) {
                hint += "+";
            } else {
                hint += "*";
            }
        }

        return hint;
    }
}

Option 2 - Use StringBuilder:

public class OtherClass
{
    private StringBuilder hint;
    private String solution = "HARPS";
    private int wordLength = 5;

    public String getHint(String theGuess) {
        hint = new StringBuilder();

        for (int index = 0; index < wordLength; index++) {
            if (theGuess.charAt(index) == solution.charAt(index)) {
                hint.append(theGuess.charAt(index));
            } else if(solution.indexOf(theGuess.charAt(index)) > 0) {
                hint.append('+');
            } else {
                hint.append('*');
            }
        }

      return hint.toString();    
    }
}
Comments