I am not totaly sure my code for my revealLetter is correct....I can't quite convert letter string into an char array so i just left it as hiddenWord == letter in the code....

package program.p02;

public class WordHider {

    private String hiddenWord;
    private String partiallyFoundWord;
    private int NUMBER_MISSES = 5;

    public int revealLetter(String letter) {
        //reveals letter stored in the parameterized string.
        int numOfTimesLetterAppears = 0;

        for (int i = 0; i < hiddenWord.length(); i++) {
            //compare letter of hiddenWord at i with letter
            //String s = new String(hiddenWord.charAt(i));
            if (hiddenWord == letter){
                //partiallyFoundWord[i] = letter; // changes astrix to letter
                numOfTimesLetterAppears++; //increment
            }//end if
        }//end for loop

        return numOfTimesLetterAppears;

    }//end revealLetter()

    public boolean wordFound() {
        //checking to see if word is guessed or not
        if (NUMBER_MISSES > 5) {
            System.out.println("You Lose. Try Again.");
            return true;
        }//end if
        else {
            System.out.println("Yay");
        }//end else

        return false;
    }//end wordFound()

    public void hideWord(char[] charArrray) {
        //resets partiallyFoundWord to all asterisks.
        char[] charArray = new char[hiddenWord.length()];
        for (int i = 0; i < hiddenWord.length(); i++) {
            charArrray[i] = '*';
            partiallyFoundWord = new String(charArray);
        }//end for loop
    }//end hideWord()

    public String getHiddenWord() {
        return hiddenWord;
    }//end getHiddenWord()

    public void setHiddenWord(String hiddenWord) {
    }//end setHiddenWord()

    public String getPartiallyFoundWord() {
        return partiallyFoundWord;
    }//end getPartiallyFoundWord()
}//end WordHider class

Recommended Answers

All 3 Replies

big mistake, quite basic to the OOP principles. the == operator is not used to check for equality of values of Objects. if you want to check whether or not two Strings have an identical value, you need to use the .equals method.
if you want to convert hiddenword to a char array:

char[] hiddenWordArray = hiddenWord.toCharArray();

just check the String api, there are a lot of methods in there you'll be able to use.

okay thank you. I did put the .equals method in there once and it was fine but I had altered something and my friend was like it wasn't what he was asking for because i think i changed the parameter from string to something else. But thank you!! :)

even if it 's not a String, as long as it's an Object, not a primitive, if you want to check equality of values, you need to use the equals method

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.