Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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 990 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.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 Sad ");
                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.
Offline ra4king

JGO Kernel


Medals: 347
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:
1  
2  
3  
4  
5  
6  
7  
             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.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
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
1  
2  
3  
4  
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 Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (36 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (41 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (60 views)
2014-09-21 02:42:18

BurntPizza (30 views)
2014-09-21 01:30:30

moogie (35 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (52 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org 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‑gaming.org
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!