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 trialEvan Fishback
9,534 PointsTreestory 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.
2 Answers
Alex Bratkovskij
5,329 PointsHeya 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
9,534 PointsThank you! Yes I figured it out earlier and was missing the import java.io.console
Alex Bratkovskij
5,329 PointsHahaha! Best of luck!
Evan Fishback
9,534 PointsEvan Fishback
9,534 PointsHere is my code