Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable assertions?

Tags:

java

assertion

In my online Java programming class I have to write a program where the user enters their age, and checks if it's between 0 and 125 - if it isn't, it shows an error code, and I need to use assertions to do that. Here is my code:

import java.util.Scanner;

public class eproject1 {

   public static void main(String args[]) {

      Scanner input = new Scanner(System.in);

      System.out.print("What is your age? ");

      int num = input.nextInt();

      System.out.println(num);

      assert(num >= 1 && num < 125);

      System.out.printf("You entered %d\n", num); // NEED TO FIX, SOMETHING ISN'T RIGHT

   }

}

The problem is that it tells you what age you entered, even if it's out of the range 1 to 124. Research says that I need to actually enable assertions, but the results I found online were very unhelpful as they involved some Eclipse stuff - I'm sure it's some kind of rookie mistake, since i'm not very good at programming.

So ... how do you get the assertion stuff to work?

like image 920
randomguy537 Avatar asked Dec 12 '14 02:12

randomguy537


1 Answers

java -ea <program_name> should do it on the command prompt.

You can try the following for intellJ and eclipse respectively.

like image 100
nitishagar Avatar answered Oct 12 '22 17:10

nitishagar