2

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.

Tot Zam
  • 8,406
  • 10
  • 51
  • 76
Bvare
  • 31
  • 4
  • What error messages are you getting? What output are you currently getting? – Tot Zam Nov 02 '16 at 01:54
  • For (theGuess.charAt(index) Eclipse tells me "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." – Bvare Nov 02 '16 at 01:59

2 Answers2

1

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 if 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();    
    }
}
Community
  • 1
  • 1
Tot Zam
  • 8,406
  • 10
  • 51
  • 76
  • Concatenating the string won't work as he needs it to only show the hint from that guess, not it and all previous guesses. – Sub 6 Resources Nov 02 '16 at 02:30
  • @sub6resources Fixed that concern by moving the `hint` variable inside the `getHint()` method. It will now reset to a blank string each time the `getHint()` method is called. `StringBuilder` can also be used, yet it seems like the OP wants a simple beginner solution. – Tot Zam Nov 02 '16 at 02:33
0

This code should work:

public String getHint(String theGuess) {
    StringBuilder hintBuilder = new StringBuilder(hint);
    for (int index = 0; index < wordLength; index++) {
        if (theGuess.charAt(index) == solution.charAt(index)) {
            hintBuilder.setCharAt(index, theGuess.charAt(index));

        } else if(theGuess.charAt(index) == solution.indexOf(string)) {
            hintBuilder.setCharAt(index, "+");
        } else {
            hintBuilder.setCharAt(index, "*");
        }
    }
    return hintBuilder;
}

Basically, you have to use a 'StringBuilder' because Strings are immutable, meaning that they cannot be altered once they are built. Also, when comparing two values, use == or === to compare, not =.

UPDATED

I forgot that Strings are immutable in Java, and have updated the code so that it should work.

Sub 6 Resources
  • 1,674
  • 15
  • 31
  • 1
    It is still telling me that hint.charAt(index) is not a variable, so I still get an error. Otherwise, it looks good! – Bvare Nov 02 '16 at 02:13
  • Ah yes, I forgot that you cannot alter Java strings after they are created. You have to create a new string with the character replaced. I'll update with new code in a minute. – Sub 6 Resources Nov 02 '16 at 02:17
  • This won't work as you can't assign to a method call. – Octavia Togami Nov 02 '16 at 02:18
  • This solution still does address all the problem with the code and it doesn't seem to compile. – Tot Zam Nov 02 '16 at 02:40
  • I wasn't trying to make it compile, simply taking the code in the question, and changing it so that there are no errors; that is what the question asked for. – Sub 6 Resources Nov 02 '16 at 02:42
  • @sub6resources This code still will not work. I added a StringBuilder option to [my answer](http://stackoverflow.com/a/40371553/4660897) with an explanation of all the parts that need to be fixed. – Tot Zam Nov 02 '16 at 03:10
  • My code may work for this specific question. Let us just wait until the person asking the question tests our methods, and figures out what works best for them. They can then accept whatever answer works. – Sub 6 Resources Nov 02 '16 at 03:13