Java Java Basics Perfecting the Prototype Parsing Integers

Evan Fishback
Evan Fishback
9,533 Points

Treestory error

Just returning back to the Java course after awhile and think I borked my workspace at some point keep getting TreeStory.java:1: error: class, interface, or enum expected
public static void main(String[] args) {
^
TreeStory.java:4: error: class, interface, or enum expected
String ageAsString = console.readLine("How old are you? ");
^
TreeStory.java:5: error: class, interface, or enum expected
int age = Integer.parseInt(ageAsString);
^

and the same error on every other line after.

Evan Fishback
Evan Fishback
9,533 Points
public static void main(String[] args) {
  Console console = System.console();

  String ageAsString = console.readLine("How old are you? ");
  int age = Integer.parseInt(ageAsString);
  if (age < 13) {
    //Insert exit code
    console.printf("Sorry you must be at least 13 to use this program.\n");
    System.exit(0);
  }
  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 TreeStory:\n--------------\n");
  console.printf("%s is a %s %s.   ", name, adjective, noun);
  console.printf("They are always %s %s.\n", adverb, verb);

}

Here is my code

2 Answers

Alex Bratkovskij
Alex Bratkovskij
5,327 Points

Heya Evan,

Welp, fist of all there is no class constructor, Remember class constructor must be the same name as the name of your file (apart from the extention, thats the bit that comes after .).

And console wasnt imported into your program, so Java doesnt know what you're talking about when you write

 console.printf(".."); 

so u need to import a library, it usually goes at the very top before the constructor.

Your code should more or less look like:

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!
        */
// __Name is a __adj__ __noun__. They are always __adv__ __verb__.
     String agAsString = console.readLine("How old are you?  ");
      int age = Integer.parseInt(agAsString);
      if (age < 13) {
        //Instert exit code
        console.printf("Sorry u must be at least 13 to use programm :@\n");
        System.exit(0);
      }

' Dont worry, we all have been there :) Hope I helped. Have fun and dont give up!

-Alex

Evan Fishback
Evan Fishback
9,533 Points

Thank you! Yes I figured it out earlier and was missing the import java.io.console