Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (707)
Games in Android Showcase (206)
games submitted by our members
Games in WIP (781)
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 1302 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: 470
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.

Galdo (284 views)
2017-01-12 13:44:09

Archive (447 views)
2017-01-02 05:31:41

0AndrewShepherd0 (907 views)
2016-12-16 03:58:39

0AndrewShepherd0 (844 views)
2016-12-15 21:50:57

Lunch (979 views)
2016-12-06 16:01:40

ral0r2 (1209 views)
2016-11-23 16:08:26

ClaasJG (1310 views)
2016-11-10 17:36:32

CoffeeChemist (1332 views)
2016-11-05 00:46:53

jay4842 (1414 views)
2016-11-01 19:04:52

theagentd (1222 views)
2016-10-24 17:51:53
List of Learning Resources
by elect
2016-09-09 09:47:55

List of Learning Resources
by elect
2016-09-08 09:47:20

List of Learning Resources
by elect
2016-09-08 09:46:51

List of Learning Resources
by elect
2016-09-08 09:46:27

List of Learning Resources
by elect
2016-09-08 09:45:41

List of Learning Resources
by elect
2016-09-08 08:39:20

List of Learning Resources
by elect
2016-09-08 08:38:19

Rendering resources
by Roquen
2016-08-08 05:55:21 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!