-2

I have a string array holding words that I am converting to a char array. Also, I am taking users input as a string, then converting it to a char and adding it to an array. I am trying to check user input against the word and if it matches just println correct or incorrect. I am not certain how to code it.

private String[] wordsForGuessing = new String[] {"one", "david", 
                                         "storage", "unhelpful"};
private String ranWord = randomWord(wordsForGuessing);
private char[] convertRanWordToACharArray = ranWord.toCharArray();

private int MAX_SIZE = countsHowManyLettersAreInAWord(ranWord);
//adding three extra turns to guess
private char[] letter = new char[MAX_SIZE + 3];

private String guess;

//Check user input to see if the letter is in the word
private void checkUserInputToRanWord(char[] word, String guess)
{
    String message = "This is ";

    for(int i = 0; i < word.length; i++)
    {
        if(guess.charAt(0) == word[i])
        {
            message += " correct guess";
        }else{
            message += " incorrect guess";
        }
    }

}
tkruse
  • 10,222
  • 7
  • 53
  • 80
  • 1
    If your requirement is just to check userinput with word of array then what is need of to convert into char array ? – Mak Jun 23 '19 at 03:42
  • The code you posted appears to be missing several methods. – Tim Biegeleisen Jun 23 '19 at 03:42
  • @Tim, I didn't include a few methods because they are irrelevant to what i am trying to do. – user8378504 Jun 23 '19 at 04:18
  • @Mak, I thought I would try to try to loop over both arrays looking for for a match but it did not work. – user8378504 Jun 23 '19 at 04:19
  • Define "did not work". What are you doing precisely (i.e. what is the word to guess, what is the string entered by the user), what do you expect to happen precisely (i.e. what should the output be), and what happens instead precisely (i.e. what is the actual output)? – JB Nizet Jun 23 '19 at 06:18
  • so you re just trying to implement hangman? https://stackoverflow.com/questions/4988143 https://stackoverflow.com/questions/26269532 https://stackoverflow.com/questions/34213468 https://stackoverflow.com/questions/23347554 https://stackoverflow.com/questions/7847735 https://stackoverflow.com/questions/37423159 – tkruse Jun 23 '19 at 07:22
  • private String guess; You are using == on a String to a character, try storing it as a character in memory – Joe Jun 23 '19 at 07:28

1 Answers1

0

There are already questions with answers on how to check whether a thing is part of an array, as an example: How do I determine whether an array contains a particular value in Java?

The general pattern with a loop is to leave the loop on the first match using break;

boolean found = false;
for (int i = 0; i < word.length; i++)
{
    if (guess.charAt(0) == word[i])
    {
        found = true;
        break;
    }
}
if (found) {
    ...
}

however modern java you can reduce this to one line using streams and anyMatch(); as in the linked answer.

Also I recommend that you look at the existing implementations of Hangman in Java that you can find on this site (linked on the above right) and others and learn from those.

tkruse
  • 10,222
  • 7
  • 53
  • 80