Java Java Basics Perfecting the Prototype Reviewing Our Feedback

My 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);

}

}

Please 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

3 Answers

Hello, 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

yes 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.

Paul Huynh i never experienced a bug in the work spaces. glad u mange the get thourgh it

Had the same problem, thanks for this.

Sajan Stanley
Sajan Stanley
987 Points

For 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.