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 trialPaul Huynh
320 PointsMy program is not reading the if statement
// So i have tried everything, yes I did save the program before running it. I have even messed with the age value but everytime i run it, it is not detecting the if statement or printing out "Sorry you must be at least 13 to use this program." . It just skips over it and asks me "Enter your name: " . Below is my code. Thank you treehouse community
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!
*/
int age = 11;
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 your name: ");
String adjective = console.readLine("Enter an ajective: ");
String noun = console.readLine("Enter an noun: ");
String adverb = console.readLine("Enter an adverb: ");
String verb = console.readLine("Enter an verb ending in -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);
}
}
3 Answers
<noob />
17,062 PointsHello, You didnt followed the videos properly. First you need to delcare a String variable that stores an input from the user :
String ageAsString = console.readLine("How old are u? ");
then you need to declare a int variable and convert the string into an int, you have to convert the string into a int because your going to check if the number the user as entered is valid, and if u notice you store that input in a string and because of that u need to convert it into a int like that :
int age = Integer.parseInt(ageAsString);
after that u do your check :
if(age < 13)
{
console.printf("Sorry u must be at least 13 to use this program !.\n");
System.exit(0);
}
please mark by question as best answer :D
Paul Huynh
320 Pointsyes that is what is told to do after this exercise but the video i was on had us hard code it. age is hard coded to 12 so age should not even go pass the if statement. Anyways i fixed it by messing with workspace and it happen to work once i physically retyped "clear javac TreeStory.java && java TreeStory" rather than pressing the up key to scroll through the history. Not only that i deleted all the code and it still ran the previous program even after saving. I am started to believe there are bugs in workspace. Which is a big no no for the learning process for beginners.
<noob />
17,062 PointsPaul Huynh i never experienced a bug in the work spaces. glad u mange the get thourgh it
ifrian
419 PointsHad the same problem, thanks for this.
Sajan Stanley
987 PointsFor compiling and executing your code, instead of using 'UP' arrow to re-copy the old code, type the whole thing again and execute. It worked for me.
Paul Huynh
320 PointsPaul Huynh
320 PointsPlease excuse the website but there are supposed to be two close brackets at the bottom as you can see the treehouse code program didnt catch everything and left some code outside the colorful code