0

I am making a java program similar to hangman game.

the program first generates a random Word from my string Array.

then the user must type a letter. if the Word has the letter in it, it asks for Another letter, until the player has typed all letters correct.

if the letter the user typed, doesn't fit in in the Word, then the the program is supposed to add a +1 to a int variable named, letterWrong. when letterWrong is 2 the image should change. when its 3 it should change when its 4 it should change when its 5, game is over.

here is the issue: when i type a letter, the program loops and checks wheter the Word contains that letter. if it does, then the program shows the letter.

the issue comes when the letter doesn't fit that Word. because, i must make sure that the word doesn't contain that letter and if it doesn't then i must add a +1 to the int variable letterWrong. problem is the +1 is within the loop. i must have it outside the loop. but i cant fix that.

here's the code:

char word = edittext.getText().charAt(0);
for(int i = 0; i<randomWord.length(); i = i+1){
    if(randomWord.charAt(i)==word){

        textviewArray[i].setText(""+randomWord.charAt(i));
    }
}

what is the best way, to check wheter the word contains that particular letter, and if it doesnt, then it should add a +1 to the int variable. the way i did it Before, it adds more than +1 to the int variable since its within the loop.

4
  • 2
    Please delete the comment above as it will not prevent down-votes and might encourage them. Instead of making such posts, please try to improve your question, such as by posting a valid minimal reproducible example with code that we can compile, run and modify. Also, you've posted your code here with your question and not in a link -- good for you! If you decide to create and post a valid MCVE, please do the same -- post your code text here with your question and not in a link. Commented Nov 6, 2016 at 14:17
  • 1
    Not my down-vote by the way -- I will wait a little while to see if you post a decent MCVE. Commented Nov 6, 2016 at 14:20
  • Did you get it to work? Commented Nov 6, 2016 at 14:36
  • yes!!! thanks alot. it took me several hours! Commented Nov 6, 2016 at 14:50

3 Answers 3

4

You can use String.contains():

String a =String.valueOf(word);
String s = randomWord;
if(s.contains(a)){
    //preform actions
...

This will make word to a string. I didn't write the whole code so as to leave room for adaptation.

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

5 Comments

Not your down-voter, but my reading of the question suggests that he might have a variable scope issue, but for me it's still hard to tell exactly where or what the problem is, and so I think that answers at this stage may be premature.
And wrong. But who cares about the result of String#contains() anyway, right? :D
@HovercraftFullOfEels I don't entirely see it like that. It is clear from the question that OP wants to check if a letter is contained in a word. This is an option...
@Tom I don't exactly understand what you mean :/
@adamH, if this answer is good, you should accept it, so that others know it works. (there's a tick under the answer score, if you press it, the answer is accepter)
0

If I got it right you can try this:

char word = edittext.getText().charAt(0);
boolean flag = false;
if(randomWord.indexOf(word ) > -1) {
   textviewArray[i].setText(""+word);
 } else {
    ++letterWrong;
   flag = true;
}

if(flag && letterWrong > 1) {
    changeImage();
}

Comments

0

As Itamar Green said, you can use String.contains() method to check if randomWord contains the letter. If it does contain the letter, show the letter. If it doesn't, increment letterWrong.

char word = edittext.getText().charAt(0);

if(randomWord.contains(word){
   textviewArray[i].setText(""+randomWord.charAt(i));
} else {
   letterWrong++;
}

Comments

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.