Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (636)
Games in Android Showcase (178)
games submitted by our members
Games in WIP (687)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Hangman, Exception error  (Read 1167 times)
0 Members and 1 Guest are viewing this topic.
Offline CJ

Junior Newbie

« Posted 2012-04-07 02:37:58 »

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.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++)
        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)
            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(;
            geraden =;
            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 );
                        geradenWoord2 += geradenWoord.charAt(i);
                    geradenWoord = geradenWoord2;
                System.out.println("Good Guess!!");
                System.out.println( geradenWoord );
                System.out.println("Wrong Guess Sad ");

        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(
   at galgje.Galgje.main(
Java Result: 1

Hope this will clear it up.

Thanks in advanced.
Offline ra4king

JGO Kernel

Medals: 409
Projects: 3
Exp: 5 years

I'm the King!

« Reply #1 - Posted 2012-04-07 05:21:21 »

Please use code tags.

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

Offline philfrei
« Reply #2 - Posted 2012-04-07 08:37:19 »

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:
                  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.

music and music apps:
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CJ

Junior Newbie

« Reply #3 - Posted 2012-04-07 12:19:28 »

Excuse me for my noobishness Smiley

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

Thanks for your help guys!!!

PS.Ra4king, I promis you, from now on I will put my code in tags Smiley
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Dwinin (70 views)
2015-11-07 13:29:08

Rems19 (80 views)
2015-10-31 01:36:56

Rems19 (72 views)
2015-10-31 01:32:37

williamwoles (106 views)
2015-10-23 10:42:59

williamwoles (92 views)
2015-10-23 10:42:45

Jervac_ (106 views)
2015-10-18 23:29:12

DarkCart (134 views)
2015-10-16 00:58:11

KaiHH (116 views)
2015-10-11 14:10:14

KaiHH (155 views)
2015-10-11 13:26:18

BurntPizza (171 views)
2015-10-08 03:11:46
Rendering resources
by Roquen
2015-11-13 14:37:59

Rendering resources
by Roquen
2015-11-13 14:36:58

Math: Resources
by Roquen
2015-10-22 07:46:10

Networking Resources
by Roquen
2015-10-16 07:12:30

Rendering resources
by Roquen
2015-10-15 07:40:48

Math: Inequality properties
by Roquen
2015-10-01 13:30:46

Math: Inequality properties
by Roquen
2015-09-30 16:06:05

HotSpot Options
by Roquen
2015-08-29 11:33:11 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!