next up previous contents
Next: Sort Class Up: MyUtil Package Previous: Notify Interface

Parse Class

The Parse class will eventually contain a variety of methods which parse String objects in a particular manner. It currently contains an intFollowing() method which scans a String and returns the integer following the requested label. For example, the following call would set x to 15.

   int x = intFollowing("Fred is 15 isn't he?","Fred is",1,0);

The GraphPanel classes use the intFollowing() method to scan a String for font= and radii= entries.

package MyUtil;

public class Parse extends Object {

   public static int intFollowing(String str, String label, 
                                  int first, int bad) {

   // returns the int following 'label' in 'str'
   // or 'bad' if 'label' does not appear in 'str'

      int a,b,l,sl,ans;
      if (str==null || label==null)
         return bad;
      if (-1==(a=str.indexOf(label,first)))
         return bad;
      sl=str.length();
      l=label.length();
      a+=l;
      for(b=a;b<sl && !Character.isDigit(str.charAt(b));b++);
      for(;b<sl && Character.isDigit(str.charAt(b));b++);
      try { ans = Integer.parseInt(str.substring(a,b)); }
      catch (NumberFormatException ex) { return bad;}
      return ans;
   }
}



Kelly Waters
Mon Oct 27 18:18:15 EST 1997