Java Java Basics Perfecting the Prototype Parsing Integers

Harold Hicks
Harold Hicks
467 Points

So I found something

I am using my Terminal to create the if age is under 13 system.exit(0) and as I ran it I found as it was loading If I typed 11 before it asked me what my age was I could type 11 once more and it would run without exiting even know im under the age. I would like to know why.

import java.io.Console;

public class Hello {

public static void main(String[] args) {
    Console console = System.console();
    String ageOfPerson = console.readLine("What is your age?  ");
  int age = Integer.parseInt(ageOfPerson);
  if (age < 13)
  {
    console.printf("Sorry you must be at lease 13 to use this APP.");
    System.exit(0);
  }
    /*  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 name = console.readLine("Enter your Name :  ");
  String ag = console.readLine("Enter Adjective :  ");
  console.printf ("%s is very %s", name , ag);

}

}

4 Answers

Miroslav Kovac
Miroslav Kovac
9,948 Points

Hmm, work for me in my local environemt, I did not use Workspaces.....

Scanner in = new Scanner(System.in);
in.nextLine();
System.out.print("What is you age: ");
String ageOfPerson = in.nextLine();

Maybe try to clear Scanner before input with in.nextline()

Miroslav Kovac
Miroslav Kovac
9,948 Points

Maybe you can try to use Scanner for input.

Scanner in = new Scanner(System.in);
System.out.print("What is you age: ");
String ageOfPerson = in.nextLine();
Ronald Williams
Ronald Williams
Java Web Development Techdegree Graduate 25,018 Points

Maybe this is an issue with workspaces. I made a java file with your code in a plain text editor and ran it through my terminal. When you run it on the terminal, it loads so fast that you can not enter 11 before it prompts with the question.

Harold Hicks
Harold Hicks
467 Points

I dont know what clear Scanner means