Hi, I just started learning java, so my skills are quite limited and I have yet to learn many many concepts. Right now, I am trying to create a hangman game(which as you can see is still incomplete). The problem I get is, the word being "mystery" when I guess my first guess with the letter "m" or any other letter, it implements the letter and rewrites "m******", but when I do my second guess, I get,
Exception in thread "AWT-EventQueue-1" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.substring(String.java:1934)
at Hangmangamee$1.actionPerformed(Hangmangamee.java:52)
at javax.swing.Timer.fireActionPerformed(Timer.java:271)
at javax.swing.Timer$DoPostEvent.run(Timer.java:201)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
I'm not sure what the problem is and why I get this error, but I think it has something to do with my guess22,guess2, etc getting mixed up.
Please help me, as I have tried for far too long to figure this out. Thankyou a ton! xXcode is belowXx

import java.awt.*;
import java.util.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*; 	//need this for the timer to work.

public class Thehangman extends Applet {
	int guess22 = 10;
	int guess3;
	public void init() {
		System.out.println("Welcome to Armaan's amazing hangman game!");

		ActionListener al = new ActionListener()
			public void actionPerformed(ActionEvent evt)
				String s = "mystery";//the hangmanword
				int temp = s.indexOf('y');
				String tempStr = "";//temporary string where stars go
				Scanner b = new Scanner(System.in);//scanner for typing guesses
				int charleft = s.length();//charleft is length of word
				if (guess22 == 10)//you have ten guesses
					for(int i = 0; i < s.length(); i++)//as long as 'm' is less then the length of the word(which it is) add ++?'s
						tempStr = tempStr + "?";
				guess22--;//remove an available guess
				System.out.println(tempStr);//print the ?'s but updated with 1less star
				boolean penalty = true;//if the penalty is actually going to occur

				String guess = b.next();
				 char guess2 = guess.charAt(0);
				for(int i = 0; i < s.length(); i++)
					if(s.charAt(i) == guess2)
						tempStr = tempStr.substring(0, i) + guess2 + tempStr.substring(i+1); 
						penalty = false;
				if(penalty == true)
					//penalty, draw head and stuff

		javax.swing.Timer t=new javax.swing.Timer(10, al);


	public void paint(Graphics g) {
    	g.fillRect(10, 250, 150, 20);

If you want to implement the hangman images, you should actually use different images, and simply redraw the appropriate image every turn. (i.e. we did hangman for the iphone in my iphone programming class. You can take the images from http://cs491f09.wordpress.com/2009/10/14/assignment-5/ if you want). Based on how many guesses the user got incorrect you'd draw a different image.

Anyway, as far as the game logic goes, I don't know what all this "substring + guess + other substring" stuff is. All you need is two variables for your word: 1) The game word, which does not change 2) the game word which you display (so you edit it with the correct letters as the user guesses them correctly).

When the user guesses a letter you can do the following:
1. Use the String class's indexOf method to determine whether or not the letter that the user guessed is in the word. This is easy because myString.indexOf(myChar) will return -1 if myChar is not in myString.

2. If the character was in the String, then you can use myString = myString.replace(oldchar, newchar) to replace every occurrence of the old character in myString with the new one. Be careful to say the "myString = " part because the method returns a new String, it does not modify the old String.

Also, I should mention that all of this is available knowledge if you go to http://java.sun.com/javase/6/docs/api/java/lang/String.html ... for example, if you read the "replace" methods, you will see that there is a method that takes two arguments: char oldchar and char newchar. The description states exactly what I told you: that every occurrence in the String of the old char is replaced by new char, and a new String is returned. I'm mentioning this because it's good to learn how to read documentation so that you can use libraries and you should get yourself comfortable with doing so. If you want, for example, you could probably find at least 3 other methods in the String class that would be helpful in a hangman game (had I not shown you the replace method, I mean).

Anyway, good luck finishing your assignment.

This article has been dead for over six months. Start a new discussion instead.