Welcome to Dream.In.Code
Become a Java Expert!

Join 150,064 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,771 people online right now. Registration is fast and FREE... Join Now!




Problem in my code

2 Pages V  1 2 >  
Reply to this topicStart new topic

Problem in my code, Its on Hangman.java

word341
6 Jun, 2008 - 10:22 AM
Post #1

New D.I.C Head
*

Joined: 6 Jun, 2008
Posts: 6

E:\Hangman2.java:8: cannot find symbol
symbol : class Hangman
location: class Hangman2
private Hangman hang;
^
E:\Hangman2.java:26: cannot find symbol
symbol : class Hangman
location: class Hangman2
public Hangman2(Hangman h){
^
2 errors

Tool completed with exit code 1

Thats the error and here is the code
CODE

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;



public class Hangman2 extends JFrame implements ActionListener{
        private Hangman hang;
        private JPanel pnlData;
        private JButton btnNew;
        private JButton btnQuit;
        private JButton [] btnA = new JButton[26];
        private int countWrong;
        private String[] guessedLetters = new String[26];
        private int guessedCount = 0;

        private JPanel pnlOutput;
        private JLabel lblTextTop;
        private JLabel lblOutputTop;
        private JLabel lblTextBottom;
        private JPanel pnlText;
        private JLabel lblOutputBottom;
        private String a;
        private String result;

        public Hangman2(Hangman h){
                super("GamePlayGUI");
                setSize(750,300);
                setLocation(100,400);
                a= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                Container c= getContentPane();
                hang = h;
                pnlText= new JPanel();
                pnlOutput= new JPanel();
                pnlData= new JPanel();
                pnlData.setLayout(new GridLayout(4,7));
                btnNew= new JButton("New Game");
                btnNew.addActionListener(this);
                btnQuit= new JButton("Quit");
                btnQuit.addActionListener(this);
                lblTextBottom= new JLabel("Guessed Letters");
                for (int i = 0; i<26; i++) {
                    this.guessedLetters[i] = " ";
                }
                lblOutputBottom= new JLabel("" + guessedLetters());
                pnlText.add(lblTextBottom,BorderLayout.NORTH);
                pnlText.add(lblOutputBottom,BorderLayout.SOUTH);
                c.add(pnlText, "South");
                lblTextTop= new JLabel("HANGMAN");
                lblOutputTop= new JLabel("",10);
                pnlOutput.add(lblTextTop, BorderLayout.NORTH);
                pnlOutput.add(lblOutputTop, BorderLayout.CENTER);
                c.add(pnlOutput, "North");
                pnlData.add(btnQuit);
                pnlData.add(btnNew);


                for (int i=0;i<26;i++){
                        btnA[i]= new JButton(a.substring(i,i+1));
                        btnA[i].addActionListener(this);
                        pnlData.add(btnA[i]);
                        c.add(pnlData, "Center");
                }

                setVisible(true);
        }

        private void guessedWrong(int y){
            int x= y;
            if (x==0) {
                this.lblTextTop.setText("You Have Six Wrong Guesses Left!");
            }
            if (x==1) {
                this.lblTextTop.setText("You Have Five Wrong Guesses Left!");
            }
            if (x==2) {
                this.lblTextTop.setText("You Have Four Wrong Guesses Left!");
            }
            if (x==3) {
                this.lblTextTop.setText("You Have Three Wrong Guesses Left!");
            }
            if (x==4) {
                this.lblTextTop.setText("You Have Two Wrong Guesses Left!");
            }
            if (x==5) {
                this.lblTextTop.setText("You Have One Wrong Guess Left!");
            }

            if (x>5) {
                this.lblTextTop.setText("GAME OVER!");
                this.lblOutputTop.setText("The Word is "+hang.getSecretWord());
                this.lblTextBottom.setText("To Play Again Select New Game");
                this.lblOutputBottom.setText("");
            }

        }


        private String guessedLetters(){
            String result = "";
            for (int i = 0; i < 26; i++) {
                result += this.guessedLetters[i];
            }
            return result;
        }


        public void actionPerformed(ActionEvent e){
                for(int i=0; i<26;i++){
                        if (e.getSource()==btnA[i]){
                                btnA[i].setEnabled(false);
                                this.guessLetter(a.substring(i,i+1));
                                hang.inputLetter(a.substring(i,i+1));
                                lblOutputTop.setText(hang.getGuessedStuff());
                                hang.gamePlay();
                                this.guessedWrong(hang.getCountWrong());
                                this.getifHangmanComplete(hang.checkComplete());
                        }
                }

                        if (e.getSource()==btnQuit){

                                this.guessedWrong(6);
                                hang.quitHangman();
                                lblOutputTop.setText("The Word is " +hang.getSecretWord());
                                lblTextBottom.setText("To Play Again Select New Game!");
                                lblOutputBottom.setText("");
                                this.clearEverything();


                        }
                        if (e.getSource()==btnNew){
                            for (int i=0;i<26;i++) {

                                btnA[i].setEnabled(true);

                            }
                                hang.clearHang();
                                this.clearEverything();

                                this.guessedWrong(0);
                                lblTextBottom.setText("Guessed Letters");
                                lblOutputBottom.setText("");
                                hang.getList();
                                hang.getRandomWord();
                                hang.inputSecretWord();
                                lblOutputTop.setText(hang.outputArray());
                                lblOutputBottom.setText("");

                        }

        }


        public void getifHangmanComplete(int complete){
                if (complete==1){
                        this.lblTextTop.setText("YOU WIN!");
                        this.lblTextBottom.setText("TO PLAY AGAIN SELECT NEW GAME");
                        this.lblOutputBottom.setText("");
                }
        }




        public void clearEverything(){
            result="";
            guessedCount=0;
            for (int i=0; i<26;i++) {
                guessedLetters[i]="";
            }
        }

        public void guessLetter(String letter){
            this.guessedLetters[guessedCount] = letter;
            this.guessedCount++;
            result="";
            for (int i = 0; i<26; i++) {
                result += this.guessedLetters[i];
            }
            this.lblOutputBottom.setText("" + result);

        }

}


This post has been edited by word341: 6 Jun, 2008 - 10:43 AM
User is offlineProfile CardPM
+Quote Post

gl3thr0
RE: Problem In My Code
6 Jun, 2008 - 10:34 AM
Post #2

D.I.C Head
**

Joined: 27 Oct, 2007
Posts: 209



Thanked: 3 times
My Contributions
okay.. so u like the big text yey...
please use these to post ur code code.gif

its hard to read as there is no formating sad.gif


bt after looks like you're trying to use a class that hasnt been created yet "Hangman"?



This post has been edited by gl3thr0: 6 Jun, 2008 - 10:42 AM
User is offlineProfile CardPM
+Quote Post

word341
RE: Problem In My Code
6 Jun, 2008 - 10:44 AM
Post #3

New D.I.C Head
*

Joined: 6 Jun, 2008
Posts: 6

I have a quick question. How do you replace seperate letters in a string? That's another problem I'm having. And what do you mean by that im trying to use a class that hasnt been created? haha sorry im dumb

This post has been edited by word341: 6 Jun, 2008 - 10:48 AM
User is offlineProfile CardPM
+Quote Post

gl3thr0
RE: Problem In My Code
6 Jun, 2008 - 10:57 AM
Post #4

D.I.C Head
**

Joined: 27 Oct, 2007
Posts: 209



Thanked: 3 times
My Contributions
this line here:

private Hangman hang;

is saying create a private variable of type "Hangman" with name "hang" however the class "Hangman" hasn't been made.

ie your class is called "Hangman2"
so if i wanted to make a variable for your class id go
private Hangman2 hang2;

so either you haven't written the class Hangman and compiled it. its not in the same directory as the Hangman2 class, or you've spelt something wrong


----edit-----
and the string thing. well i've never needed 2 do it be4 so theres proly a better way to do this BUT.
you could pt it into a char array and then change a value and pt it back into the string?

you could create a temp copy of the string. set the orig to empty then write a for loop to add the chars from the temp string back in. (during the for loop u might say if (i==2) {org += "a"} this would then change the specified letters)

a little more advance way might be 2:
use CharSequence to copy everything be4 the character to be changed. add that 2 a new string and the new letter then copy everything after it?

or finally u can use the replace method but thts hard to work with if u have more then one of the same character
java

String sts = "hello";
System.out.println(sts);
sts = sts.replace('e', 'l'); //replaces e with l
System.out.println(sts)


This post has been edited by gl3thr0: 6 Jun, 2008 - 11:17 AM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Problem In My Code
6 Jun, 2008 - 11:19 AM
Post #5

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Your class is called Hagman2 not Hangman

I guess you want:

private Hangman2 hang;

public Hangman2(Hangman2 h)

This is the egg and the hen ... you need an object of type Hangman2 to create a Hangman2
or you have another class Hangman somewhre else ? If this is the case make sure that the compiles version of Hangman.java the file Hangman.class is in the same directory than Hangman2.java



User is online!Profile CardPM
+Quote Post

word341
RE: Problem In My Code
6 Jun, 2008 - 11:22 AM
Post #6

New D.I.C Head
*

Joined: 6 Jun, 2008
Posts: 6

QUOTE(pbl @ 6 Jun, 2008 - 12:19 PM) *

Your class is called Hagman2 not Hangman

I guess you want:

private Hangman2 hang;

public Hangman2(Hangman2 h)

This is the egg and the hen ... you need an object of type Hangman2 to create a Hangman2
or you have another class Hangman somewhre else ? If this is the case make sure that the compiles version of Hangman.java the file Hangman.class is in the same directory than Hangman2.java



ohhh so all i need to do is add that and the program would work?
User is offlineProfile CardPM
+Quote Post

gl3thr0
RE: Problem In My Code
6 Jun, 2008 - 11:29 AM
Post #7

D.I.C Head
**

Joined: 27 Oct, 2007
Posts: 209



Thanked: 3 times
My Contributions
QUOTE(word341) *

ohhh so all i need to do is add that and the program would work?


no
the class Hangman has a bunch of methods that Hangman 2 does not seem to have
ie:
getGuessedStuff()
getSecretWord()
checkComplete()
outputArray()
clearHang()
and more..

This post has been edited by gl3thr0: 6 Jun, 2008 - 11:30 AM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Problem In My Code
6 Jun, 2008 - 11:46 AM
Post #8

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(gl3thr0 @ 6 Jun, 2008 - 12:29 PM) *

the class Hangman has a bunch of methods that Hangman 2 does not seem to have
ie:
getGuessedStuff()
getSecretWord()
checkComplete()
outputArray()
clearHang()
and more..


Ok so there is a Hangman and I guess that Hangman2 is the GUI for Hangman.

So I guess that

private Hangman hang;

Hangman2(Hangman h)
...
hang = h;

is correct.

So just make sure Hangmass.class is in the same directory than Hangman2.java before compiling Hangman2.java

User is online!Profile CardPM
+Quote Post

word341
RE: Problem In My Code
6 Jun, 2008 - 11:58 AM
Post #9

New D.I.C Head
*

Joined: 6 Jun, 2008
Posts: 6

yeah like i tried compiling it once and it worked when i took the private hangman hang out and i just got the keyboard down this is the only part i need to get working, so basically i just gotta make sure its in the same directory and ill be fine right?
User is offlineProfile CardPM
+Quote Post

gl3thr0
RE: Problem In My Code
6 Jun, 2008 - 12:06 PM
Post #10

D.I.C Head
**

Joined: 27 Oct, 2007
Posts: 209



Thanked: 3 times
My Contributions
ya.
can we see the code for hangman?
User is offlineProfile CardPM
+Quote Post

word341
RE: Problem In My Code
6 Jun, 2008 - 12:18 PM
Post #11

New D.I.C Head
*

Joined: 6 Jun, 2008
Posts: 6

its the first msg i posted on the forum the code is there haha
User is offlineProfile CardPM
+Quote Post

pbl
RE: Problem In My Code
6 Jun, 2008 - 12:19 PM
Post #12

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(word341 @ 6 Jun, 2008 - 01:18 PM) *

its the first msg i posted on the forum the code is there haha

You seem confused... it is Hangman2 that you posted ! haha
User is online!Profile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:42PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month