Hangman, Exception error

Hi there,

Im busy with a Hangman game.
A strange error keeps me from finishing it. I’ve looked into it but still I don’t get it.

I hope one of you can help me.

Here is the code:

package galgje;

import java.io.IOException;
import java.util.*;

/**
*

  • @author CJ
    */
    public class Galgje {

    /**

    • @param args the command line arguments
      */
      public static void main(String[] args) throws IOException {

      int aantalKeerGeraden = 0;

      String geraden;

      String letterCheck = “This letter is incorrect!”;

      StringBuffer geradenLetters = new StringBuffer();

      String hetWoord = “fakkeldrager”;
      int lengteWoord = hetWoord.length();

      String geradenWoord = “”;
      String geradenWoord2;

      for (int i = 0; i<lengteWoord;i++)
      {
      geradenWoord+=".";
      }

      String galg[] = new String[10];

      galg[0] = “\n| \n| \n| \n| \n|_ \n\n";
      galg[1] = "_____\n| \n| \n| \n| \n|
      \n\n";
      galg[2] = "_____\n|/ \n| \n| \n| \n|
      \n\n";
      galg[3] = "_____\n|/ |\n| \n| \n| \n|
      \n\n";
      galg[4] = "_____\n|/ |\n| 0\n| \n| \n|
      _\n\n”;

      galg[5] = “\n|/ |\n| 0\n| |\n| \n|_ _\n\n";
      galg[6] = "
      \n|/ |\n| 0\n| /|\n| \n|_ \n\n";
      galg[7] = "_____\n|/ |\n| 0\n| /|\\n| \n|
      \n\n";
      galg[8] = "_____\n|/ |\n| 0\n| /|\\n| / \n|
      \n\n";
      galg[9] = "_____\n|/ |\n| 0\n| /|\\n| / \\n|
      _\n\n”;

      while (aantalKeerGeraden<10)
      {
      geradenWoord2="";
      System.out.println(galg[aantalKeerGeraden]);
      System.out.println(geradenWoord + “\n”);
      System.out.println("The Letters you have guessed are : " + geradenLetters);
      System.out.println(“Enter some characters.”);
      Scanner inScan = new Scanner(System.in);
      geraden = inScan.next();

       geradenLetters.append(geraden + ", ");
       
       if(hetWoord.indexOf(geraden) != -1)
       {
           for (int i = 0; i < hetWoord.length( ); i++)
           {
               if( hetWoord.charAt( i ) == geraden.charAt( 0 ) )
                   geradenWoord2 += geraden.charAt( 0 );
               else
                   System.out.println(geradenWoord.charAt(i));
                   geradenWoord2 += geradenWoord.charAt(i);
           
               geradenWoord = geradenWoord2;
           }
           
           hetWoord.indexOf(geraden);
           System.out.println("Good Guess!!");
           System.out.println( geradenWoord );
      
       }else
       {
           System.out.println("Wrong Guess :( ");
           aantalKeerGeraden++;
       }
      

      }

      if (aantalKeerGeraden==9)
      {
      System.out.println(“YOU’VE LOST!”);
      }
      if (aantalKeerGeraden<9)
      {
      System.out.println(“CONGRATULATIONS YOU’VE WON!”);
      }

    }
    }

When I run this I get an error when guessing a letter.
The strange thing is it doesn’t give an error on every letter.
The letter ‘a’ is throwing an error but the letters q and p.

the error is as followed:

Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(String.java:695)
at galgje.Galgje.main(Galgje.java:72)
Java Result: 1

Hope this will clear it up.

Thanks in advanced.

Please use code tags.

And please point out where line 72 is. Also your code is very confusing and has many errors :S

Hi - Welcome to JGO!

As ra4king points out, it would be easier to read your code if you put it within [c ode][/c ode] “code” tags.

As to the bug in your code, I would suggest taking a closer look at two things:

  1. In your if…else construction, you haven’t put the code in {}, so only the first line is considered to be part of the else (at this point, only the println statement). The rest will always be executed, regardless of the indentation.

  2. When you do this: geradenWoord = geradenWoord2; you shorten geradenWoord to a string that is single char in length. Thus, when i = 1, and it points to the second character, it is out of range because geradenWoord is only one char long.

Perhaps one or the other is not exactly your intention?

Do you have access to a debugger? I think stepping through the code would go a long way towards clearing up what is going awry.

I made a guess and the following seems to help:

             else
             {
                  System.out.println(geradenWoord.charAt(i));
                  geradenWoord2 += geradenWoord.charAt(i);
             }
         }
         geradenWoord = geradenWoord2;

But I think you also need a way to test for success in order to break out of your while loop before 9 bad guesses.

Excuse me for my noobishness :slight_smile:

Thanks for your replies.

placing the geradenWoord = geradenWoord2 out of the for loop made it work.
Also added the { } tags.

I will make a game win result something like this


if(hetWoord.equals(geradenWoord))
{
break;
}

Thanks for your help guys!!!

PS.Ra4king, I promis you, from now on I will put my code in tags :slight_smile: