Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialhaulata aziz
314 Pointsi cant compile my code in NetBeans ! please help
when i run my program i brings an error
String answerToLife = "42";
1 Answer
Matthew Caloger
12,903 PointsIf I understand your issue correctly, when you run your file in NetBeans, are you trying to run only String answerToLife = "42";
?
While that will run in the TreeHouse environment, in a real Java environment you'll need more around that to have it run.
Here is a full "Hello World" Java application.
The code below needs to go into "HelloWorld.java", with any Java file, the <filename>.java needs to match the class name (as shown below with public class HelloWorld
). Don;t worry too much about what public class HelloWorld
means, just know that a full Java application needs to be wrapped in one.
Next is the public static void main(String[] args) {
statement. in Java, the main method is the entry point to the program. You'll learn about the public static void
and (String[] args)
parts later, disregard them for now.
Inside of the main curly-braces { }, you put the code that'll run. So String answerToLife = "42";
would go inside of there.
public class HelloWorld {
public static void main(String[] args) {
// Prints "Hello, World" to the terminal window.
System.out.println("Hello, World");
}
}
For the purposes of this lesson, the following code would work:
public class ParseInts { //Remember, classname has to match the file name!
public static void main(String[] args) {
//Code goes in here
String answerToLife = "42";
int answer = parseInt(answerToLife); // this completed the task the lesson is asking for, it creates an int variable called "answer" and gets the number representation of the string)
//Code ends here
}
}
Getting started with Java can be a little confusing at first, but you'll get the hang of it.
Here's a Quickstart guide for NetBeans that may be of help.
Additionally, for future posts, please include all code and error messages as it allows us to help the most :).