11

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?

randomguy537
  • 165
  • 1
  • 1
  • 6

2 Answers2

24

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

You can try the following for intellJ and eclipse respectively.

Community
  • 1
  • 1
nitishagar
  • 8,480
  • 3
  • 22
  • 36
0

run> edit configurations > modify options > Add VM options: input -ea to VM options