The Java Program: Parse.java

  1 // Parse.java
  2 
  3 import java.lang.Integer;
  4 import java.lang.NumberFormatException;
  5 
  6 class Parse {
  7 
  8    final static String [] good = { "1", "23", "023", "-432", "0000"};
  9    final static String [] bad  = { "0x1", "AB", "123F", "  432", "00 00",
 10                                    "+123", "932 ", "   ", ""};
 11 
 12    public static void test (String [] s) {
 13       for (int i=0; i<s.length; i++) {
 14          try {
 15             final int n = Integer.parseInt (s[i]);
 16             System.out.println ("'"+s[i]+"' is the integer " + n);
 17          } catch (NumberFormatException e) {
 18             System.out.println ("'"+s[i]+"' is not an integer");
 19          }
 20       }
 21    }
 22 
 23    public static void main (String[] args) {
 24       System.out.println ("The good:");
 25       test (good);
 26       System.out.println ("The bad:");
 27       test (bad);
 28       System.out.println ("The ugly:");
 29       test (args);
 30    }
 31 }