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 trial

Java

Code not running at all

This is what I entering into the console to run my code: clear && javac TreeStory.java && java TreeStory

Please advise what I am doing wrong.

Code: import java.io.Console;

public class TreeStory {

public static void main(String[] args) { Console console = System.console(); /* Some terms: noun - Person, place or thing verb - An action adjective - A description used to modify or describe a noun Enter your amazing code here! */ String ageAsString = console.readLine("How old are you? "); int age = Integer.parseInt(ageAsString);

if (age <8){ // ad system exit code console.printf ("Sorry you have to at least 15 to use this program.\n"); System.exit (0); }//close if 1

String name = console.readLine("Enter a name: "); String adjective = console.readLine("Enter an adjective: "); String noun = console.readLine("Enter a noun: "); String adverb = console.readLine("Enter an adverb: "); String verb = console.readLine("Enter a verb ending with -ing: ");

console.printf ("-----------Your Story----------- \n"); console.printf ("%s is a %s %s. ",name, adjective,noun); console.printf ("He is always %s %s.",verb,adverb);

}//close main

1 Answer

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

If I just copy pasted your code in Workspaces, and run it. It turned out with this error:

Picked up JAVA_TOOL_OPTIONS: -Xmx128m                                                                    
TreeStory.java:13: error: reached end of file while parsing                                              
}//close main                                                                                            
 ^                                                                                                       
1 error          

Do you have the same error ?

If you do, you should add one more closing brace after main method, brace closing your class.

Here is the code that worked. Hope it is the same as yours ...

import java.io.Console;

public class TreeStory {
    public static void main(String[] args) {
        Console console = System.console(); 
        /* Some terms: noun - Person, place or thing verb - 
        An action adjective - 
        A description used to modify or describe a noun 
        Enter your amazing code here! */

        String ageAsString = console.readLine("How old are you? ");
        int age = Integer.parseInt(ageAsString);

        if (age < 8){
            // ad system exit code 
            console.printf ("Sorry you have to at least 15 to use this program.\n");
            System.exit (0);
        } //close if 1

        String name = console.readLine("Enter a name: ");
        String adjective = console.readLine("Enter an adjective: ");
        String noun = console.readLine("Enter a noun: ");
        String adverb = console.readLine("Enter an adverb: ");
        String verb = console.readLine("Enter a verb ending with -ing: ");

        console.printf ("-----------Your Story----------- \n");
        console.printf ("%s is a %s %s. ",name, adjective,noun);
        console.printf ("He is always %s %s.",verb,adverb);

    }//close main
// the brace that was forgotten
}

If however your error is different, please post the error as well.

Also would be nice if you could edit your question and use markdown cheatsheet, to post your code in code block :)

Receiving the message below after adding the brace.

Picked up JAVA_TOOL_OPTIONS: -Xmx128m
Picked up _JAVA_OPTIONS: -Xmx128m
How old are you? Exception in thread "main" java.lang. at java.lang.NumberFormatException.forInputStr at java.lang.Integer.parseInt(Integer.java:592 at java.lang.Integer.parseInt(Integer.java:615 at TreeStory.main(TreeStory.java:12)

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

It look to me that you hit Enter twice.

You see when you type:

clear && javac TreeStory.java && java TreeStory

You have to press Enter only once and wait.

Otherwise, because you type Enter second time, program actually reads your input. And because the first line where user is prompted "How old are you?", the program takes Enter as argument.

How did I know that, because Integer.parseInt method throws NumberFormatException if argument passed to parseInt was not a number, in this case argument is the input you typed, saved in ageAsString.

Let me illustrate this with the code. I will modify your code, so that if you hit Enter twice, you will get message printed, that ageAsString is not a number.

import java.io.Console;

public class TreeStory {
    public static void main(String[] args) {
        Console console = System.console(); 
        /* Some terms: noun - Person, place or thing verb - 
        An action adjective - 
        A description used to modify or describe a noun 
        Enter your amazing code here! */

        String ageAsString = console.readLine("How old are you? ");
        int age = 0;
        try {
          age = Integer.parseInt(ageAsString);
        } catch (NumberFormatException nfe) {
          System.out.println("ageAsString you entered: '" + ageAsString + "' is not a number");
          // could also print here stack trace, the one that you saw in console
          System.exit(1);
        }

        if (age < 8){
            // ad system exit code 
            console.printf ("Sorry you have to at least 15 to use this program.\n");
            System.exit (0);
        } //close if 1

        String name = console.readLine("Enter a name: ");
        String adjective = console.readLine("Enter an adjective: ");
        String noun = console.readLine("Enter a noun: ");
        String adverb = console.readLine("Enter an adverb: ");
        String verb = console.readLine("Enter a verb ending with -ing: ");

        console.printf ("-----------Your Story----------- \n");
        console.printf ("%s is a %s %s. ",name, adjective,noun);
        console.printf ("He is always %s %s.",verb,adverb);

    }//close main
// the brace that was forgotten
}