I am almost done with this Hangman game, but I am having problems with the letter buttons and am lost as to what to do to fix the problem...

package hangman;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;

public class Hangman extends JFrame implements ActionListener {

    private static int WIDTH=1000;
    private static int HEIGHT=750;



    private JButton exitB;



    private Container pane;

    private String[] easy={"dog", "cat", "hot", "yes", "run"};
    private String[] normal={"door", "calm", "fish", "year", "stun"};
    private String[] hard={"truck", "fight", "mouse", "radio", "atone"};

    private boolean usd[] = new boolean[26];

     private String guessme=easy[0];

     private int numguesses=0;

     private boolean finished = false;

     private boolean won = false;


     private JButton a[];
     private JRadioButton easyRB, normalRB, hardRB;



    public Hangman() {
        setTitle("Hangman");
        setSize(WIDTH, HEIGHT);


        pane=getContentPane();

        int i;
        StringBuffer buffer;




        a = new JButton[26];

        // create all 26 buttons
        for (i = 0; i <26; i++) {
            buffer = new StringBuffer();
            buffer.append((char)(i+65));
            a[i] = new JButton(buffer.toString());
            a[i].addActionListener( this );
            a[i].setSize(50, 30);
            if(i<13)a[i].setLocation(i*50, 300);
            else a[i].setLocation((i-13)*50, 330);
            pane.add(a[i]);
        }





        exitB=new JButton("Exit");
        easyRB=new JRadioButton("Easy");
        easyRB.setSelected(false);
        normalRB=new JRadioButton("Normal");
        normalRB.setSelected(true);
        hardRB=new JRadioButton("Hard");
        hardRB.setSelected(false);

        ButtonGroup group = new ButtonGroup();
        group.add(easyRB);
        group.add(normalRB);
        group.add(hardRB);
        easyRB.setLocation(800, 10);
        normalRB.setLocation(800, 40);
        hardRB.setLocation(800, 70);

        easyRB.setSize(80, 30);
        normalRB.setSize(80, 30);
        hardRB.setSize(80, 30);
        easyRB.addActionListener(this);
        normalRB.addActionListener(this);
        hardRB.addActionListener(this);



        pane.setLayout(null);


        exitB.setSize(200, 30);
        exitB.setLocation(800, 600);
        exitB.addActionListener(this);

        pane.add(easyRB);
        pane.add(normalRB);
        pane.add(hardRB);
        pane.add(exitB);





        repaint();
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);


    }//end Hangman

    public void paint(Graphics g) {
        //draw gallows and rope
        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);


 g.setColor(Color.orange);

 if (numguesses >=1 )
        g.drawOval(82,95,30,30);

 g.setColor(Color.green);

 if (numguesses >=2 )
        g.drawLine(97,125,97,150);

 if (numguesses >=3 )
        g.drawLine(97,150,117,183);

 if (numguesses >=4 )
        g.drawLine(97,150,77,183);

 if (numguesses >=5 )
        g.drawLine(97,125,117,135);

 if (numguesses >=6 )
        g.drawLine(97,125,77,135);

        StringBuffer st = new StringBuffer();

        for (int l=0; l<=25; l++) {
                if (usd[l]) st.append((char)(l+65));
                else st.append(".");
        }
        g.setColor(Color.blue);

        Font f = new Font("Courier",Font.ITALIC,14);

        g.setFont(f);
        g.drawString(st.toString(),25,285);

        StringBuffer guessed = new StringBuffer();

        Font ff = new Font("Courier",Font.BOLD,24);
        g.setColor(Color.black);

        g.setFont(ff);

        for (int mm=0;mm<guessme.length();mm++) {
                if (usd[(int)guessme.charAt(mm)-65])
                        guessed.append(guessme.charAt(mm));
                else
                        guessed.append(".");
                }

        g.drawString(guessed.toString(),75,230);



     if (numguesses >=6) {
        g.setColor(Color.white);
        g.fillRect(70, 200, 200, 30);
        g.setColor(Color.black);
        g.drawString(guessme.toString(),75,230);
        Font fff = new Font("Helvetica",Font.BOLD,36);
        g.setFont(fff);

        g.setColor(Color.red);
        g.drawString("You lose!",200,100);

        finished = true;
        }

     if (won) {
        Font fff = new Font("Helvetica",Font.BOLD,36);
        g.setFont(fff);

//        Color red=new Color.red
        g.setColor(Color.red);

        g.drawString("You Win!",200,100);
        finished = true;
        }





}




     public void rer(int lett) {

           if (!finished) {

                boolean found=false;
                boolean www=false;

                if (!usd[lett]) {
                        for (int mm =0;mm<guessme.length();mm++) {
                                if (guessme.charAt(mm)==((char)(lett+65))) found=true;
                        }
                        if (!found) numguesses++;

                }

                usd[lett] = true;

                for (int mm =0;mm<guessme.length();mm++) {
                      if (!usd[(int)(guessme.charAt(mm))-65]) www=true;
                }

                if (!www) won=true;        


                repaint();

           }

         }


            public void actionPerformed( ActionEvent ev) {
             if(ev.getActionCommand().equals("Exit")){
                 System.exit(0);
             }

             if(easyRB.isSelected()){
                 guessme=easy[0];

             }
             if(normalRB.isSelected()){
                 guessme=normal[0];

             }
             if(hardRB.isSelected()){
                 guessme=hard[0];

             }


                int i;
              for (i = 0; i < 26; i++) {
                if (ev.getSource() == a[i]) { rer(i); }
              }



        }



    public static void main(String[] args)  {
        Hangman demo =new Hangman();
    }



    }

what exactly is the problem/ what are you trying to do? Do you have any errors when compiling or running?

[edit]
actually got the problem fixed add:

@Override
public void paint(Graphics g) {
    super.paint(g); 
        //other code
        }

as your first line in your paint(Graphics g) {} method

Edited 4 Years Ago by DavidKroukamp

Instead of overriding the frame's paint method, you should create a class that extends JPanel and override its paintComponent method

public void rer(int lett) {
if (!finished) {
boolean found=false;
boolean www=false;
if (!usd[lett]) {
for (int mm =0;mm<guessme.length();mm++) {
if (guessme.charAt(mm)==((char)(lett+65))) found=true;
}
if (!found) numguesses++;
}
usd[lett] = true;
for (int mm =0;mm<guessme.length();mm++) {
if (!usd[(int)(guessme.charAt(mm))-65]) www=true;
}
if (!www) won=true;
repaint();
}

I think I am still having a problem with this part of the code.

Do you get any error messages when you execute the code?

Why do you use the value 65 so many times in the code?

The 65 is for the ascii values. The code compiles but when I use any of the letter buttons in the gui the console outputs an array out of bounds exception.

an array out of bounds exception.

Please copy the full text of the error message and post it here.

Look at the line where the exception occurs and see why the index's value is past the end of the array.

Please explain what you mean by:

The 65 is for the ascii values.

The ASCII table is a list of values beginning with zero that correspond to different characters. 65 is the numeric representation of the capital letter A, 66 is B, 67 is C and so forth.

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 35
    at hangman.Hangman.paint(Hangman.java:177)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.access$700(Unknown Source)
    at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 35
    at hangman.Hangman.paint(Hangman.java:177)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.access$700(Unknown Source)
    at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 35
    at hangman.Hangman.rer(Hangman.java:239)
    at hangman.Hangman.actionPerformed(Hangman.java:272)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 35
    at hangman.Hangman.paint(Hangman.java:177)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.access$700(Unknown Source)
    at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 35
    at hangman.Hangman.paint(Hangman.java:177)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.access$700(Unknown Source)
    at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 35
    at hangman.Hangman.paint(Hangman.java:177)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.access$700(Unknown Source)
    at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 35
    at hangman.Hangman.paint(Hangman.java:177)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.access$700(Unknown Source)
    at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

Why not code 'A' instead of 65? It would make the code more readable.

java.lang.ArrayIndexOutOfBoundsException: 35

Why does the code use an index of 35 at line 177? The largest array has 26 elements.

I don't know about the 35 and why it's there. I can't find that happening in the code anywhere.

Add a println statement to your code to print out the value of the index used on line 177.
Print out the values of all the variables used to compute the index on that line.

im not sure also whats up with the code either but i got it working: change the -35 to -97

if (usd[(int) guessme.charAt(mm)-97])

and where ever else the 35 is used in the 'usd' array, which is twice.

Edited 4 Years Ago by DavidKroukamp

Comments
-97 is poor coding style use 'a'

97 is 'a', so this looks like an upper case / lower case problem. Probably best to convert the words and the user input to a consistent case before trying to match letters etc.

ps chars are numeric values in Java, so rather than obscure code like this
guessme.charAt(mm)-65
you could code the much more obvious
guessme.charAt(mm)-'A'

@NormR1, as i stated i wasnt sure why it worked by putting a 97, because at first i used a 100. so thank for the minus 1. anyway atleast now i know

at first i used a 100

That is because you did not understand what the code was doing. Recommending the use of 97 does not help anyone understand what the code was doing. It was computing an index into an array of lower case letters. the first letter in the array is 'a' so its index is the desired letter - 'a'.
That is the problem with using int values with no meaning, Using the char value: 'a' or 'A' makes the code more readable and easier to understand. To index into an array of lower case letters, subtract 'a', if uppercase letters: subtract 'A'

Edited 4 Years Ago by NormR1

That is because you did not understand what the code was doing.

true, well i knew it was searching for a letter?! :P

Recommending the use of 97 does not help anyone understand what the code was doing.

I never gave an understanding i gave a fix-which does work?

anyhow difference in judgements let them be. atleast OP has working code which he understands

i gave a fix-which does work

I disagree with giving code to OPs without explanations. They do not learn from copy and paste.
Posting code without helping the OP understand the code is not helping the OP to learn how to solve problems.

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