Hello, i have came across a problem when trying to create a hangman game.
I want to read a String of letters inputed from keyboard and then i want someone else to guess this word(typical hangman rules). I have used this code.

InputStreamReader FindWord = new InputStreamReader(System.in);
      BufferedReader in = new BufferedReader(FindWord);
      String letter = aLetter.getText();
      FindWord.indexOf(letter);      
      int charPos = FindWord.indexOf(letter);
      if (charPos == 0) Char1.setText(letter);     
      if (charPos == 1) Char2.setText(letter);
      if (charPos == 2) Char3.setText(letter);
      if (charPos == 3) Char4.setText(letter);
      if (charPos == 4) Char5.setText(letter);
      if (charPos == 5) Char6.setText(letter);
      FindWord.indexOf(letter, charPos+1);

The problem is that its says that

indexOf

"can not find symbol
symbol: method indexof(java lang string)
location: class java io input stream reader"
hmmm i'm not really sure how to overcome this problem, could you suggest solution for that ??

What this is telling you is that the InputStreamReader does not define an "indexOf" method. This makes sense, since you're talking about a stream, not a persisent entity.

It looks like your indexOf is trying to find the position of the input character in the target String - this would be correct, since indexOf is definitely available as a method of String.

I hope this points you in the right direction.

Ok i've managed to read a String input from keyboard, but there is a little problem and i dont know where, It freezes after trying out first letter, so i.e Secret Word Type into console = Hello (saved), When entering letter "H" in GUI it shows the position of that letter, and it is shown in format like H _ _ _ _, But when i type other letter i.e "O" the program freezes, and i have to break the build

private void TryLetterActionPerformed(java.awt.event.ActionEvent evt) {                                          
      String FindWord;
      Scanner in = new Scanner(System.in);
      FindWord = in.nextLine();
      String letter = aLetter.getText();      
      int charPos = FindWord.indexOf(letter);
      charPos = FindWord.indexOf(letter);
      MyMessage.setText("position is " + charPos);
      
      if (charPos == 0) Char1.setText(letter);
      if (charPos == 1) Char2.setText(letter);
      if (charPos == 2) Char3.setText(letter);
      if (charPos == 3) Char4.setText(letter);
      if (charPos == 4) Char5.setText(letter);
      if (charPos == 5) Char6.setText(letter);
      charPos = FindWord.indexOf(letter, charPos+1);

Edited 5 Years Ago by infinitus: n/a

it seems like it can pnly read one letter from that string ?, what im missing ??

Is it as simple as mismatched case? In your example, you're matching "O" against 'o' - that's going to come up false. Have you tried 'o' as a guess?

it still freezes, seems like it can only read one letter from the word stored in string ... What sort of code should i write to allow the program to read all letters from the word stored in a string ?

private void TryLetterActionPerformed(java.awt.event.ActionEvent evt) {                                          
      String FindWord;
      Scanner in = new Scanner(System.in);
      FindWord = in.nextLine();
      String letter = aLetter.getText();      
      int charPos = FindWord.indexOf(letter);
      charPos = FindWord.indexOf(letter);
      MyMessage.setText("position is " + charPos);
      
      if (charPos == 0) Char1.setText(letter);
      if (charPos == 1) Char2.setText(letter);
      if (charPos == 2) Char3.setText(letter);
      if (charPos == 3) Char4.setText(letter);
      if (charPos == 4) Char5.setText(letter);
      if (charPos == 5) Char6.setText(letter);
      charPos = FindWord.indexOf(letter, charPos+1);

Okay, let's go through this. Each time this is called, you make a new Scanner (wasteful - your Scanner really ought to be persistent) and you use it to enter a word, called "FindWord". Then you get a letter from "aLetter" which is evidently a textfield or some other text-bearing component. Then you get the letter's position in the word you've just given it, and you put that number in a message to the user, if that number is between 0 and 5, you put the letter into some Swing component and then you look to see if the character appears a second time.


I don't know what's going on in the rest of your code, but that's what's happening here.
Nothing here that should hang, but there's a lot of things that smell funny in terms of design (for example, why are you getting the word you're searching in as input each time someone tries a guess?).

Edited 5 Years Ago by jon.kiparsky: n/a

yeah i dont want to input word each time someone tries to guess, i just want to input it once and so its stored, then user can guess letters from that word and eventually if he find them all he win or make to many mistakes game over. This is where i'm having problem, how to input the word once not every time someone tries to guess.

ok i've done some polishing up of the code and it look like that... but it still freezes after first try of the letter...
I have removed the canner just to see if i can get it going without entering word at the begining but already having a word in a variable FindWord;

String FindWord = "hello";
     
      String letter = aLetter.getText();      
      int charPos = FindWord.indexOf(letter);
      charPos = FindWord.indexOf(letter);
      MyMessage.setText("position is " + charPos);
      int entry = 0;
      for (entry =0;entry<10;entry++)
      {
      int incorrect = 0;
      if (charPos == 0) Char1.setText(letter);
      if (charPos == 1) Char2.setText(letter);
      if (charPos == 2) Char3.setText(letter);
      if (charPos == 3) Char4.setText(letter);
      if (charPos == 4) Char5.setText(letter);
      if (charPos == 5) Char6.setText(letter);
      if (charPos == -1)
      {
        incorrect = incorrect - 1;
      }
          if (incorrect == -1)
          {
             JButton1.setIcon(new javax.swing.ImageIcon("C:\\Users\\Boss\\Desktop\\NetBeans Java programs\\Hangman CW\\src\\image\\1.jpg"));
          }
          if (incorrect == -2)
          {
             JButton1.setIcon(new javax.swing.ImageIcon("C:\\Users\\Boss\\Desktop\\NetBeans Java programs\\Hangman CW\\src\\image\\2.jpg"));
          }
          if (incorrect == -3)
          {
             JButton1.setIcon(new javax.swing.ImageIcon("C:\\Users\\Boss\\Desktop\\NetBeans Java programs\\Hangman CW\\src\\image\\3.jpg"));
          }
          if (incorrect == -4)
          {
             JButton1.setIcon(new javax.swing.ImageIcon("C:\\Users\\Boss\\Desktop\\NetBeans Java programs\\Hangman CW\\src\\image\\4.jpg"));
          }
          if (incorrect == -5)
          {
             JButton1.setIcon(new javax.swing.ImageIcon("C:\\Users\\Boss\\Desktop\\NetBeans Java programs\\Hangman CW\\src\\image\\5.jpg"));
          }
          if (incorrect == -6)
          {
             JButton1.setIcon(new javax.swing.ImageIcon("C:\\Users\\Boss\\Desktop\\NetBeans Java programs\\Hangman CW\\src\\image\\7.jpg"));
          }
        }
      
    }

What is happening now, it finds letters but it doesnt change pictures... if let say u guessed wrong twice...
And if any one have a solution to how to make this working when entering word when program runs please could you give me me advice thank you :)

Hello

I was wondering how did you solve it as i am doing this for fun and was wondering how you worked it out.

This question has already been answered. Start a new discussion instead.