Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Basics Perfecting the Prototype Censoring Words - Looping Until the Value Passes

Class, interface or enum expected?

To Whom It May Concern, Here is my code and error messages. I've looked online and looked at the code several times. Please provide a hint or something as to why it's not compiling.

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 __adjective__ __noun__. They are always __adverb__ __verb__.
          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;
          do {
             noun = console.readLine("Enter a noun: ");
           if  (noun.equalsIgnoreCase("dork") || 
                      noun.equalsIgnoreCase("jerk"))   {
               console.printf("That language is not allowed. Try again. \n\n");
             }
            } while (noun.equalsIgnoreCase("dork") || noun.equalsIgnoreCase("jerk"));
          String adverb = console.readLine("Enter an adjective: ");                                             
            if (isInvalidWord) {
                    noun.equalsIgnoreCase("moneychaser");}
                                console.printf("That God Dern language isn't allowed. Try Again . \n\n");
             }
         while (isInvalidWord);             
            String adverb = console.readLine("Enter an adjective: ");
            String verb = console.readLine("Enter a verb ending in -ing: ");

            console.printf("Your TreeStory:\n--------------\n");
            console.printf("%s is a %s %s. ", name, adjective, noun);
          if (noun.equals("dork")) {
            console.printf("That God Dern language isn't allowed. Exiting. \n\n");
            System.exit(0);
            }
            console.printf("They are always %s %s. \n", adverb, verb);

TreeStory.java:46: error: class, interface, or enum expected
console.printf("They are always %s %s. \n", adverb, verb);
^
TreeStory.java:47: error: class, interface, or enum expected

I have a curly brace under the last line as well.

I think that use of console went out of scope, take a look at your curly braces

1 Answer

Cameron Nilon
Cameron Nilon
15,601 Points

Hi Ted,

You mentioned that there is another curly brace to close the main, but do you have another for the class that is wrapping main?

If this error is still giving you trouble, this is what I cooked up note I didn't use java.io.console instead java.util.Scanner so it will look a bit different from the follow along.

import java.util.Scanner;

public class MadLib {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("ENTER YOUR AGE:");
        int age = input.nextInt();
        if (age < 13) {
            System.out.println("You must be at least 13 years of age.\n");
            System.exit(0);
        }

        input.nextLine();
        System.out.println("ENTER YOUR NAME:");
        String name = input.nextLine();

        System.out.println("ENTER AN ADJECTIVE:");
        String adjective = input.nextLine();

        String noun;
        boolean isInvalidWord;
        do {
            System.out.println("ENTER AN NOUN:");
            noun = input.nextLine();
            isInvalidWord = (noun.equalsIgnoreCase("dork") || 
                                     noun.equalsIgnoreCase("jerk"));
            if (isInvalidWord) {
                System.out.printf("%s is not allowed. TRY AGAIN...\n", noun);
            }

        } while(isInvalidWord);

        System.out.println("ENTER AN ADVERB:");
        String adverb = input.nextLine();

        System.out.println("ENTER A VERB:");
        String verb = input.nextLine();

        System.out.printf("YOUR STORY\n--------------\n");
        System.out.printf("%s, is a %s %s.  ", name, adjective, noun);
        System.out.printf("They are allways %s %s. \n", adverb, verb);    
    }
}