• Post Reply Bookmark Topic Watch Topic
  • New Topic

Basic Java beginner help please  RSS feed

 
Jake Meyer
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys im using netbeans working through a tutorial (Sam's guide to learning java in 21 days) and i've come across a error.
My code is below along with a screenshot attached
~Jake


class VolcanoRobot {
String status;
int speed;
float temperature;

void checkTemperature() {
if (temperature > 660) {
status = "returning home";
speed = 5;
}
}

void showAttributes() {
System.out.println("Status: " + status);
System.out.println("Speed: " + speed);
System.out.println("Temperature: " + temperature);
}
}
Code-Ranch.JPG
[Thumbnail for Code-Ranch.JPG]
Code Ranch help
 
Pallavi Sadit
Ranch Hand
Posts: 50
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is your main method?
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Netbeans probably does not know what to do as it was expecting to find a class with a main method. A main method is the entry point to a Java program. Have a look here!

http://journals.ecs.soton.ac.uk/java/tutorial/getStarted/application/anatomy.html
 
Jake Meyer
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you write me an example method for this example?
 
Blake Edward
Ranch Hand
Posts: 106
4
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main method is written as follows: public static void main(String [] args){}

The name of your class will dictate the name of the java file that contains the main method. In this case, VolcanoRobot.java should be the name of your file.

If you insert the main method in the class VolcanoRobot (between the curly braces of the class and not within the curly braces of the methods) the code will compile with no errors.
 
Blake Edward
Ranch Hand
Posts: 106
4
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Insert this and see what you get.

public static void main(String[] args){
VolcanoRobot v = new VolcanoRobot();
v.checkTemperature();
v.showAttributes();
}

This question is about default values, instance variables, and the scope in which class variables and instance variables live.
 
Jake Meyer
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies guys, i think the problem is that my files are messed up because all its is saying java21.Java21 class iwasn't found in VolcanoRobot.java
 
Jake Meyer
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Blake this is what i get...

public static void main(String[] args)
class VolcanoRobot {
String status;
int speed;
float temperature;

void checkTemperature() {
if (temperature > 660) {
status = "returning home";
speed = 5;
}
}

void showAttributes() {
System.out.println("Status: " + status);
System.out.println("Speed: " + speed);
System.out.println("Temperature: " + temperature);
}
}
same.JPG
[Thumbnail for same.JPG]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at the screenshot, you have put the main method outside of any class. As you can see there are red squiggly lines under your code, indicating that there are errors. First make sure the code is correct and compiles without errors, it makes no sense to try to run it when it cannot be compiled.

Methods must always be inside a class, not outside of any class like you have done it now. Put the main method inside the VolcanoRobot class.

Please take some time to learn the basics of how a Java program is structured:

Lesson: The "Hello World!" Application
Lesson: A Closer Look at the "Hello World!" Application
Lesson: Classes and Objects
 
Blake Edward
Ranch Hand
Posts: 106
4
Java Netbeans IDE Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trust me, there is no way to learn Java in 21 days. There is no crash course. The basic "Hello World" application is actually very useful and you can see the basic structure of a class that contains a main method and has output.

When learning Java (and I still study everyday) I use TextMate which is just a simple text editor. I find that NetBeans and Eclipse interfere with learning. Also, you need to take more chances with these code snippets. you should put the main method everywhere and see what kind of errors you get and why, as well as finding out where it belongs. No offense, but had you done this you would have seen the problem.

Go to the actual source http://docs.oracle.com/javase/tutorial/getStarted/application/ and read the definition of the main method. This should get you started in the right direction.

Self study can be difficult and you might want to trade in the book you are using for the actual Oracle tutorials, then you might consider picking up one of the Java SE 7 Associate Programmer certification books which can give you a great understanding of the basics of the language, and prepare you for the exam, if you are interested.

After almost two years of self study (off and on) I would say that most intro to java books are just snowflakes on the tip of the iceberg.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!