On Mon, Sep 30, 2002 at 01:55:20PM -0500, Jerry Jalenak wrote:

> Easy, John ... step away from the keyboard ... carefully now
> ... :-)

I'm sorry about that, I don't know why I converted DL to ZIP.
The lookup table works nicely when you know you will start by
knowing the state abbreviation. I would further recommend loading
the strings from a data file, such as a Properties file. By making
the map static, you would only do this once when the class loads.

/**
 * This class gets the regular expression validators for drivers
 * licenses for each state.
 */
public class MyClass {

  private static final String PROPFILE_NAME = "MyClass.properties";

  private static final Properties s_stateToDLRegExp = new Properties();

  static {
    InputStream inProps = null;
    try {
      inProps = MyClass.class.getResourceAsStream("/" + PROPFILE_NAME);
      s_stateToDLRegExp.load(inProps); 
    }
    catch (IOException excp) {
      throw new IllegalStateException("Could not init DL regexp");
      excp.printStackTracke(); // log the error
    }
    finally {
      if (inProps != null) {
        try {
          inProps.close();
        }
        catch (IOException excp) {
          System.err.println("Could not cleanup properties file " + PROPFILE_NAME);
          excp.printStackTrace();
        }
      }
    }
  }

  public static String getDLRegExpForState(String state) {
    // Handle nulls however you want here.
    return s_stateToDLRegExp.getProperty(state);
  }
}

> Seriously, I wasn't sure what the best way of doing this was,
> or even if it could be done. I'm still looking at the code (still
> undecided about the exceptions) and may yet end up recoding
> it as a static HashMap. I'm still learning about the in's and
> out's of reflection, and thought I'd try it this way first (always
> looking to learn something new!). Thanks for your comments though
> - and by the way, it's not zip codes, it's Drivers License numbers
> (the format of 'em, anyway!).....

Cheers,
John
-- 
end of line


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to