2008-05-29

meet with new friend, assertion from java island

damn...
kind of simple code, but today i just know...





Fig. 13.9 Checking with assert that a value is within range.
   1  // Fig. 13.9: AssertTest.java
2 // Demonstrates the assert statement 3 import java.util.Scanner; 4 5 public class AssertTest 6 {
7 public static void main( String args[] ) 8 { 9 Scanner input = new Scanner( System.in ); 10 11 System.out.print( "Enter a number between 0 and 10: " );
12 int number = input.nextInt(); 13
14 // assert that the absolute value is >= 0
15 assert ( number >= 0 && number <= 10 ) : "bad number: " + number;
16
17 System.out.printf( "You entered %d\n", number );
18 } // end main
19 } // end class AssertTest



Enter a number between 0 and 10: 5
You entered 5






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


No comments:

Google Referrals