Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes The strangest thing with this program! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The strangest thing with this program!" Watch "The strangest thing with this program!" New topic
Author

The strangest thing with this program!

Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
This is weird. I was working on this program before and it was compiling and running perfectly. I saved it, logged out of my user and went on later that day and then it wouldn't compile anymore. I don't understant why. It gives me this:

cannot find symbol class tester - line 11
cannot find symbol class tester - line 11
cannot find symbol class WindowQuitter - line 103
cannot find symbol class WindowQuitter - line 103


This is the code:



// Can somebody please help we this!!!???
[ April 06, 2006: Message edited by: Mark Spritzler ]

Live And Let Learn.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11154
    
  16

where are those two classes defined?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
tester is defined 11 lines down from the start of the code with:
tester Test1 = new tester();
and WindowQuitter is defined 103 lines down from the start of the code with:
WindowQuitter wquit = new WindowQuitter ();

Hopefully this helps!!
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
Can somebody please help me with this... asap... It just doesn't make sense to me!!!
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
tester t = new tester();

is not defining a class, it's just instantiating it. the compiler cannot find the class definition. why? probably because you are not importing it.

find your class and make sure that the compile has access to it. same with WindowQuitter.

***

according to java coding conventions, it's bad practice to name a class with a starting small letter, and also bad practice to name a variable with a starting capital letter. better use Tester as name for your class, and flick as a name for your button...
this convention makes code reading easier for everyone.

***

bye,
jan
[ April 05, 2006: Message edited by: Jan Groth ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11154
    
  16

when you say " tester Test1 = new tester();"

you are telling the JVM that you want to make a new object of type tester, and you will refer to it via the Test1 variable.

but nowhere in this code are you telling it WHAT a tester is. Java doesn't know how to make a tester object.

you need to define the class somewhere, usually in a file called tester.java. or, someone else might have written it, and you need to import it.

you're giving someone a recipie, and in the middle of it saying "now make a Espagnole sauce"...

unless you tell them how to make that sauce, or tell them where they can GET that sauce, the recipie is useless.
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
Ok. So I fixed the class problem and now the code is written:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Gui extends Frame implements ActionListener

{

Button Flicks = new Button("Flicks"); // Constructs a button.
Button Tunes = new Button("Tunes");
tester Test1 = new tester();

Gui() // The constructor.

{

setLayout (new FlowLayout() ); // Layout.
Flicks.addActionListener(this); // Register the object.
Tunes.addActionListener(this);
add(Flicks); // Adds button to the container.
add(Tunes);

}

public void actionPerformed(ActionEvent Chan) // "Chan" short for channel.

{

if (Chan.getSource() == Flicks)

{

Test1.trial(1,0);
repaint();

}

if (Chan.getSource() == Tunes)

{

Test1.trial(0,1);
repaint();

}

}

public void paint(Graphics g)

{

setBackground (Color.orange);

int xPos = ((getSize().width) / 2);

if (Test1.Test == 1)

{

setBackground (Color.orange);
g.setColor(Color.black);

g.drawString("The Lord Of The Ring", xPos - 62, 100);
g.drawString("The Matrix", xPos - 30, 130);
g.drawString("Star Wars", xPos - 29, 160);
g.drawString("The Count of Monte Cristo", xPos - 70, 190);
g.drawString("King Kong", xPos - 29, 220);

}

if (Test1.Test == 2)

{

setBackground (Color.orange);
g.setColor(Color.black);

g.drawString("Linkin Park - Faint", xPos - 56, 100);
g.drawString("Rob Zombie - Dragula", xPos - 62, 130);
g.drawString("Linkin Park - Numb", xPos - 56, 160);
g.drawString("Linkin Park - In The End", xPos - 70, 190);
g.drawString("The Spiderman Song!!", xPos - 62, 220);

}

if (Test1.Test == 3)

{

setBackground (Color.white);

}

}

class main (String[] args)

{

Gui frm = new Gui ();
frm.setResizable (false);
WindowQuitter wquit = new WindowQuitter ();
frm.addWindowListener (wquit);
frm.setSize (300, 250);
frm.setVisible (true);

}

class WindowQuitter extends WindowAdapter

{

public void windowClosing (WindowEvent e)

{

System.exit(0); // Exits the program.

}

}

class tester

{

int Test;

public void trial(int Yes, int No)

{

if (Yes == 1 && No == 0)
Test = 1;
else if (Yes == 0 && No == 1)
Test = 2;
else
Test = 3;

}

}

}



// There is a problem compiling it still. It says I need braces on line 97 and 145. Can somebody help me with my dilema?
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
My mistake... switch class main on line 97 to public void main.
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
Another mistake... put static in there as well. I can't figure out what to do!!!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11154
    
  16

PLEASE use the code tags when posting your code. it preserves the formatting and makes your code readable. Simply paste in your code, then hilight it, and press that "code" button down below. or, go back and edit your post and put
[ code] before and [ /code] after, only without the space.

Also, it would help if you could label which is line 97 and 145. it's hard to tell in the browser, and without posting the exact error message (which would at least let me search the screen for the line), it's not easy to help you.

also, i'm not really conviced this is an intermediate question...
[ April 06, 2006: Message edited by: fred rosenberger ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Moving to Java in General (Beginner).


[Jess in Action][AskingGoodQuestions]
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
// The bolded part is the problem. I don't understand whats wrong with it.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Gui extends Frame implements ActionListener

{

Button Flicks = new Button("Flicks"); // Constructs a button.
Button Tunes = new Button("Tunes");
tester Test1 = new tester();

Gui() // The constructor.

{

setLayout (new FlowLayout() ); // Layout.
Flicks.addActionListener(this); // Register the object.
Tunes.addActionListener(this);
add(Flicks); // Adds button to the container.
add(Tunes);

}

public void actionPerformed(ActionEvent Chan) // "Chan" short for channel.

{

if (Chan.getSource() == Flicks)

{

Test1.trial(1,0);
repaint();

}

if (Chan.getSource() == Tunes)

{

Test1.trial(0,1);
repaint();

}

}

public void paint(Graphics g)

{

setBackground (Color.orange);

int xPos = ((getSize().width) / 2);

if (Test1.Test == 1)

{

setBackground (Color.orange);
g.setColor(Color.black);

g.drawString("The Lord Of The Ring", xPos - 62, 100);
g.drawString("The Matrix", xPos - 30, 130);
g.drawString("Star Wars", xPos - 29, 160);
g.drawString("The Count of Monte Cristo", xPos - 70, 190);
g.drawString("King Kong", xPos - 29, 220);

}

if (Test1.Test == 2)

{

setBackground (Color.orange);
g.setColor(Color.black);

g.drawString("Linkin Park - Faint", xPos - 56, 100);
g.drawString("Rob Zombie - Dragula", xPos - 62, 130);
g.drawString("Linkin Park - Numb", xPos - 56, 160);
g.drawString("Linkin Park - In The End", xPos - 70, 190);
g.drawString("The Spiderman Song!!", xPos - 62, 220);

}

if (Test1.Test == 3)

{

setBackground (Color.white);

}

}

public static void main (String [] args
{

Gui frm = new Gui ();
frm.setResizable (false);
WindowQuitter wquit = new WindowQuitter ();
frm.addWindowListener (wquit);
frm.setSize (300, 250);
frm.setVisible (true);


}

class WindowQuitter extends WindowAdapter

{

public void windowClosing (WindowEvent e)

{

System.exit(0); // Exits the program.

}

}

class tester

{

int Test;

public void trial(int Yes, int No)

{

if (Yes == 1 && No == 0)
Test = 1;
else if (Yes == 0 && No == 1)
Test = 2;
else
Test = 3;

}

}

}
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Let me echo fred and say PLEASE use [ code] [ /code] tags.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Fritz Urling
Greenhorn

Joined: Jan 09, 2006
Posts: 13
Ok, i'll be the first to admit i get a little confused on inner classes...

but why did you make WindowQuitter an inner class of Gui? i don't see any pressing need for it to be there. in fact, if you move it out, i think your code works just fine.

Or, do this:


you might want to read this if you want to keep using an inner class.
[ April 06, 2006: Message edited by: Fritz Urling ]
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
Thanks!! Also, I enjoyed the campfire tale!
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
Ok... so I tried taking it out of the class... and failed. What exactly do I have to do to make this work?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

As fred and Garrett said above, please use Code Tags to keep your code indentation showing. This makes it much easier for others to look at your code and more likely that you will get good responses.
[ April 06, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11154
    
  16

here is your code, cleaned up:


you can see how you have defined the WindowQuitter & tester class inside the final curly brace of the Gui class. if you cut'n'paste all of the WindowQuitter class OUTSIDE that last brace, so the last part looks like this:


Normally you would put each of these classes in its own file, but well save that for another day.

besides enjoying the campfire story, can you apply what it talks about and get your code to work with tester and WindowQuitter left as inner classes???

that would be a good exercise for you.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Hint: You're very close to this compiling and running. I see 2 syntax errors (that the compiler should point out for you), and then a simple matter of correctly instantiating the inner class.
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
I'm not understanding!! This is getting really frustrating. I'm doing what you all have being saying and maybe I'm doing it wrong, but its not helping at all! Why can't I keep this inside the Gui class?
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
Ok, I understand why the other two classes have to be outside the Gui brace... now. I didn't even notice that. I know that problem. But... its still giving me errors.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Martin vanPutten:
... its still giving me errors.

What exactly are the error messages? These should be helpful in finding the problem.
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
It works! Its working perfectly! Thank you all for your help!! ^_^ I understand inner and outer classes now!!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11154
    
  16

the two classes do NOT have to be outside the Gui class. i was able to get the code to work with them inside, with on minor change (apart from the syntax error). Fritz's post even showed you what you would need to do, as does that campfire story.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: The strangest thing with this program!
 
Similar Threads
Buttons and action events
VectorCanvas
Slot Machine
Slot Machine
How to make a java applet repeat infinitently?