Atakan Bal
Atakan Bal

Reputation: 27

Java conditionals (if clauses set-up)

What I'm trying to figure out is when user puts age < 18 I want program to stop. Same idea with "Jack" part below that If user named jack I want it to stop also.

import java.util.Scanner;

class Newbie
{
  public static void main(String[] arg)
  {
    Scanner qk = new Scanner(System.in);
    int age;
    String ans;

    System.out.println("How old are you?");
    age = qk.nextInt();

    if(age < 18)
      System.out.println(age + " is too young!");           

    if(age > 18)
      System.out.println("You can enter. What is your name?");

    Scanner q = new Scanner(System.in);
    ans = q.nextLine();

    if(ans.equals("Jack"))
    System.out.println("Jack, you are not allowed to use this program.");
  }
}

Upvotes: 1

Views: 183

Answers (6)

recursion.ninja
recursion.ninja

Reputation: 5488

Consider using a boolean field to keep track of whether or not the user is authorized to use the program.

Example:

import java.util.Scanner;

class Newbie
{
    public static void main(String[] arg)
    {
      boolean authorized = true;
      Scanner qk = new Scanner(System.in);
      int Age;
      String ans;

      System.out.println("How old are you?");
      Age=qk.nextInt();
      in.nextLine(); // clear newline char from the buffer
      if(Age < 18) {
        System.out.println ( Age +" is too young! " ); 
        authorized = false;         
      }
      else { // else statement fixes logic error
        System.out.println ( " You can enter. What is your name ? " );
        ans=qk.nextLine();
        if (ans.equals("Jack")) {
          System.out.println ( "Jack, you are not allowed to use this program " );
          authorized = false;         
        }
      }
      if(authorized) {
        // Do program stuff here
      }
    }

}

Upvotes: 1

Konrad Viltersten
Konrad Viltersten

Reputation: 39268

Exiting the program execution by return works in this case, because you only have a very simple main setup. If you want to stop execution independently of what method you're in, use the following.

if(age < 18)
  System.exit(0);

In your case, it would be like this.

if(age < 18){
  System.out.println(age +" is too young!");
  System.exit(0);
}

Besides that, you've got a logical error in your code. What happenes if the user enters 18? He/she is still allowed in but has nothing that tells him/her that. Use this condition instead.

if(age < 18){
  System.out.println(age +" is too young!");
  System.exit(0);
} else {
  System.out.println("You can enter. What is your name?");
}

Upvotes: 0

Matze
Matze

Reputation: 21

Just use System.exit(0) to stop the Programm where ever you are, return; just terminate the method.

Upvotes: 2

mzedeler
mzedeler

Reputation: 4371

If you want it to stop if the user enters "Jack", do like this:

if (ans.equals("Jack")) {
    System.out.println ( "Jack, you are not allowed to use this program " );
    System.exit();
}

Upvotes: 0

superphil0
superphil0

Reputation: 94

put a "return;" after the if ( but place what you want to come after the if in curly braces {}

since you are inside the main function, the return will just exit the function, and therfore exit the program

Upvotes: 0

SLaks
SLaks

Reputation: 888185

You can write return; to terminate execution of the current method.

Upvotes: 2

Related Questions