This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Following tutorial, confused on class error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Following tutorial, confused on class error" Watch "Following tutorial, confused on class error" New topic
Author

Following tutorial, confused on class error

Greg Mark
Greenhorn

Joined: Dec 13, 2010
Posts: 8
First off, let me say:

HELLO EVERYONE!

Back in the day I tried to learn C++, but I was younger and patience wasn't my virtue. After that I tried out scripting for NWN and found that to be fun, but my computer died and I lost interest.

After playing Minecraft and Dwarf Fort recently, my interest in learning a programing language has been rekindled and it seems like Java is a good place to start. I'm 23 btw.

So heres the breakdown:

After deciding to try this out I went to the net to find a book, got myself a copy of Head First and I'm on the Guess Game Tut.

Here's what I have:


After getting 23 errors in the code I was able to widdle it down to 2 on my own. I'm using Eclipse for this. Was gonna use notpad and the JDK compiler but I couldn't figure that out on my own. This what I'm having trouble with in the code now.



Its telling me the 'The public type Player has to be defined in its own file."

Same for this (only with GameLauncher):



Now the tut doesn't specify this. I have it all in one Java doc. How do I solve this?

Now the book is done 1.5 I'm using 1.6 Is this my problem? What am I missing? Are my classes defined right? I'm also not to sure what the method and classes are here. Same for the instance variable.

Thank you to anyone who has taken the time to read this, and great thanks to anyone who takes the time to help a clueless self teaching student.

I'm looking forward to a long stay here.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Welcome to JavaRanch!

The error message that you got tells you how to solve it: "The public type Player has to be defined in its own file."

In Java, public classes must be in their own source file, which has the same name as the public class and the ".java" extension. So, don't put all the code in one source file, but create separate source files for each class (GuessGame.java, Player.java and GameLauncher.java).

It doesn't have anything to do with using Java 6 instead of Java 5.

Another very good set of tutorials is this: The Java Tutorials


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Greg Mark
Greenhorn

Joined: Dec 13, 2010
Posts: 8
I'll do that, thank you. I wasn't sure about what it meant by file.

Now, GuessGame, Player, and GameLaunch are my classes, right?

Then this here would be my instance variable?:


Then this would be my method?:


I'm still unclear on how it translates to the actual code, I understand what the terms mean, just not what it is in practice.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Have a look at the Oracle Java tutorials I posted a link to, especially the chapter Learning the Java Language, which explains the basics of classes, objects, variables, methods etc. It might be better to start with some smaller examples that focus on those basic concepts before you start working with a more complicated program - don't try to learn too much at once.

Also, please check your private messages for an administrative issue.
Greg Mark
Greenhorn

Joined: Dec 13, 2010
Posts: 8
Alright. I'll take a look and read up. Thank you for your time.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
Greg Mark wrote:I'll do that, thank you. I wasn't sure about what it meant by file.

Now, GuessGame, Player, and GameLaunch are my classes, right?

Then this here would be my instance variable? . . .
In that case, in the GuessGame class, the instance variables are P1 P2 and P3 (actually, under style conventions that should be p1 p2 p3). You appear to have three of them. The variables p1Guess, p1IsRight, etc appear to be local variables in the startGame method.

In the Player class, there is the guess variable (1 instance field) and in the GameLauncher class, you haven't got any instance variables.

When you got the Head First book, I presume you got the 2nd edition? That uses Java5 (1.5); you won't notice much difference in Java6 (1.6) at this stage.

Don't use MS Notepad; it isn't very good for programming. Try Notepad2, jEdit or Notepad++, which are all much better. Enable such options as syntax highlighting and bracket matching and automatic indentation (1 tab automatically changed to 4 spaces).
Try this on the command line
mkdir java_work [or similar]
cd java_work
[Edit the three files and save them in the java_work directory.]
javac GameLauncher.java
java GameLauncher
This will only work if you have set your PATH correctly; if it doesn't, you will find help in this Java™ Tutorials section, particularly "common problems". The first error message tells what happens if you don't set your PATH.
Don't set a system classpath.
 
jQuery in Action, 2nd edition
 
subject: Following tutorial, confused on class error