Java Java Basics Perfecting the Prototype Parsing Integers

haulata aziz
haulata aziz
314 Points

i cant compile my code in NetBeans ! please help

when i run my program i brings an error

ParsingInts.java
String answerToLife = "42";

1 Answer

Matthew Caloger
Matthew Caloger
12,903 Points

If 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 :).