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

Receiving multiple errors and code not running

Receiving the message from console:

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)

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 <13){
        // ad system exit code 
        console.printf ("Sorry you have to at least 13 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);

}

}

Not inputting an age... i just compile it and receive that error.

Seth Kroger
Seth Kroger
56,413 Points

From the output you are both compiling and running the program. Try typing in an age when prompted and see how it works.

2 Answers

Seth Kroger
Seth Kroger
56,413 Points

That error, NumberFormatException, is caused when the string you're trying to turn into a number isn't numeric, so parseInt() can't make sense of it. For example, attempting to parse "thirteen" instead of "13". What age are you inputting?

Not inputting an age.. I just compile it and receive that error.

Picked up JAVA_TOOL_OPTIONS: -Xmx128m
Picked up _JAVA_OPTIONS: -Xmx128m
How old are you? Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.parseInt(Integer.java:615)
at TreeStory.main(TreeStory.java:12)
treehouse:~/workspace$ 13
bash: 13: command not found
treehouse:~/workspace$

Said command not found

Seth Kroger
Seth Kroger
56,413 Points

There was no error compiling the program so try running it. And what was your command to compile it? You didn't include it.

command to compile is

clear && javac TreeStory.java && java TreeStory