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
Adam Derby
206 PointsPrompt not working
I am learning basic Java. For some reason, when I run this program, no matter what age I type into the prompt, it returns with the command for being too young to execute the program. Can you tell me why it won't run the program, regardless of age?
here is my code:
String ageAsString = console.readLine ("How old are you? "); int age = Integer.parseInt (ageAsString); if (age < 13); { console.printf("Sorry, wait a few years.\n"); System.exit(0); }
1 Answer
Gavin Ralston
28,770 PointsI'm posting your code here formatted so we can see it a little better. :)
String ageAsString = console.readLine("How old are you? ");
int age = Integer.parseInt(ageAsString);
if (age < 13); {
console.printf("Sorry, wait a few years.\n");
System.exit(0);
}
See that semi-colon that snuck in there in your if statement? This is what it looks like to java:
if (age < 13) {/* nothing here */};
// JAVA: Okay we're finished, we just hit the semicolon. Let's run whatever the next statement is.
{ all the stuff you put in braces gets run now, every time. }
...and that next line is everything inside of those braces, every time. :)
With the if statement, you just set your condition and immediately open curly braces to start typing out the statements you want to happen when the condition is met.
Hope that helps.