Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: assert() function is not working. Why?

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question assert() function is not working. Why?

    The following program is not working as I want. The assert() function is not working while I'm entering a number which is out of range 0-10. Whereas, according to my book, it is working like that:

    Output:
    Enter a number between 0 and 10: 50
    Exception in thread "main" java.lang.AssertionError: bad number: 50
    at AssertionTest.main(AssertTest.java:15)


    But I cannot see the error message above after compiling the program on eclipse and netbeans. I just see that:

    Output:
    Enter a number between 0 and 10: 50
    You entered 50

    What is the problem? Why the function assert is not working?

    import java.util.Scanner;
     
    public class Assertion {
     
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
     
            System.out.print("Enter a number between 0 and 10: ");
            int number = input.nextInt();
     
            assert(number >= 0 && number <= 10) : "bad number: " + number; // assert, "bildir" demektir.
     
            System.out.printf("You entered %d\n",number);
        }
     
    }


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: assert() function is not working. Why?

    assert is not a function, its a keyword. Its a build in feature that must be enabled before it can be used.

    To enable this feature you have to add the command "-ea" to the argument list when initializing your JVM.
    Your IDE should give you the option to set a Run-Configuration where you can select the JVM commands to be used when testing your application. Make sure to add the command to the correct list.

  3. The Following User Says Thank You to Cornix For This Useful Post:

    hefese (September 7th, 2014)

  4. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up Re: assert() function is not working. Why?

    Thank you so much Cornix. I did it in Eclipse and Netbeans like that:

    In Eclipse:
    Run->Run Configurations->Arguments->VM Arguments-> ....
    I wrote -ea in VM Arguments.

    In Netbeans:
    Run->Set Project Configuration->Customize->VM Options->...
    I wrote -ea in VM Options.

Similar Threads

  1. Replies: 7
    Last Post: April 25th, 2013, 01:12 PM
  2. Java simple login function not working
    By lf2killer in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 28th, 2012, 05:06 AM
  3. Working with log math function
    By colerelm in forum Object Oriented Programming
    Replies: 0
    Last Post: October 15th, 2012, 06:30 PM
  4. Deprecation in assert?
    By blobbyx22 in forum Java Theory & Questions
    Replies: 3
    Last Post: November 26th, 2011, 12:37 PM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM

Tags for this Thread