UNB/ CS/ David Bremner/ teaching/ java/ CharExample.java
public class CharExample{
  
  public static void main(String[] args){

    System.out.println(parseHexDigit('E'));

    System.out.println(parseHexDigit('9'));
    System.out.println(parseHexDigit('a'));

    System.out.println(parseHexDigit('L'));


  }
/* 
14
9
10
Exception in thread "main" java.lang.IllegalArgumentException: Illegal hex digit: L
        at CharExample.parseHexDigit(CharExample.java:36)
        at CharExample.main(CharExample.java:10)

Process CharExample exited abnormally with code 1
*/

  /**
     Convert a char representing a hexadecimal digit to an 
     int.
     @param digit Hex digit to convert 0..9, A..F
     @returns corresponding integer value.
     
     @throws IllegalArgumentException if the char is not a
     hex digit.
  */

  static int parseHexDigit(char digit){
    
    if ( '0' <= digit  && digit <= '9' ){
      return digit-'0';
    } else if ('A' <= digit && digit <= 'F') {
      return digit -'A'+10;
    } else if ('a' <= digit && digit <= 'f') {
      return digit -'a'+10;
    } else {
      throw new IllegalArgumentException("Illegal hex digit: "+
                                         digit);
    }
  }
}