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("***********************");
System.out.println("Welcome to Armaan's amazing hangman game!");
System.out.println("***********************");
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
}
System.out.println(tempStr);
repaint();
}
};
javax.swing.Timer t=new javax.swing.Timer(10, al);
t.start();
}
public void paint(Graphics g) {
setBackground(Color.white);
g.fillRect(10, 250, 150, 20);
g.fillRect(40,70,10,200);
g.fillRect(40,70,60,10);
g.setColor(Color.yellow);
g.fillRect(95,70,5,25);
}
}