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 Java Basics Perfecting the Prototype Censoring Words - Using String Equality

Magnus Sagerup
Magnus Sagerup
1,690 Points

error: <identifier> expected

Hi I dont get why I get this error

TreeStory.java:31: error: <identifier> expected
console.printf("They are always %s %s.\n", adverb, verb);

here is the 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("What is your age?");


  // int age = Integer.parseInt(ageAsString);
  if (age < 13) 
    console.printf("Sorry, you must be alteast 13 to use this program\n");
    System.exit(0);

  }
  String name = console.readLine("Enter your name  ");
  String adjective = console.readLine("Enter an adjective  ");
  String noun = console.readLine("Enter a noun  ");
  String verb = console.readLine("Enter a verb  ");
  String adverb = console.readLine("Enter a adverb  ");
  console.printf("%s is very %s", name, adjective);

  console.printf("Enter your TreeStory:\n--------\n");
  //console.printf("%s is a %s %s", name, adjective, noun);
  console.printf("They are always %s %s.\n", adverb, verb);

}

2 Answers

jordan stanley
jordan stanley
7,541 Points

Hey, you're just missing an opening curly brace for your if statement. The compiler is reading the ending curly brace for that statement as the ending curly brace for main, so all code after that is not included in the main method.

I found it here: https://stackoverflow.com/questions/8520497/compilation-error-identifier-expected

You are indeed missing an opening curly brace for your if statement as pointed out, but you're also trying to access the console instance of Console from outside the scope of where it was created.

Try moving the printf statements back inside the main method:

import java.io.Console;

public class TreeStory {

public static void main(String[] args) {
    Console console = System.console();

    String ageAsString = console.readLine("What is your age? ");
    int age = Integer.parseInt(ageAsString);

    if (age < 13) {
      console.printf("Sorry, you must be alteast 13 to use this program\n");
      System.exit(0);
    }

    String name = console.readLine("Enter your name  ");
    String adjective = console.readLine("Enter an adjective  ");
    String noun = console.readLine("Enter a noun  ");
    String verb = console.readLine("Enter a verb  ");
    String adverb = console.readLine("Enter a adverb  ");

    console.printf("%s is very %s", name, adjective);
    console.printf("Enter your TreeStory:\n--------\n");
    console.printf("They are always %s %s.\n", adverb, verb);
  }
}