I need to convert full state name to its official state address code. For example from the String New York, I need to produce NY. Now I could put this all in a hashmap, bu
As key and value:
public enum State {
AK("AK", "Alaska"),
AL("AL", "Alabama"),
AR("AR", "Arkansas"),
AZ("AZ", "Arizona"),
CA("CA", "California"),
CO("CO", "Colorado"),
CT("CT", "Connecticut"),
DE("DE", "Delaware"),
FL("FL", "Florida"),
GA("GA", "Georgia"),
HI("HI", "Hawaii"),
IA("IA", "Iowa"),
ID("ID", "Idaho"),
IL("IL", "Illinois"),
IN("IN", "Indiana"),
KS("KS", "Kansas"),
KY("KY", "Kentucky"),
LA("LA", "Louisiana"),
MA("MA", "Massachusetts"),
MD("MD", "Maryland"),
ME("ME", "Maine"),
MI("MI", "Michigan"),
MN("MN", "Minnesota"),
MO("MO", "Missouri"),
MS("MS", "Mississippi"),
MT("MT", "Montana"),
NC("NC", "NorthCarolina"),
ND("ND", "NorthDakota"),
NE("NE", "Nebraska"),
NH("NH", "NewHampshire"),
NJ("NJ", "NewJersey"),
NM("NM", "NewMexico"),
NV("NV", "Nevada"),
NY("NY", "NewYork"),
OH("OH", "Ohio"),
OK("OK", "Oklahoma"),
OR("OR", "Oregon"),
PA("PA", "Pennsylvania"),
RI("RI", "RhodeIsland"),
SC("SC", "SouthCarolina"),
SD("SD", "SouthDakota"),
TN("TN", "Tennessee"),
TX("TX", "Texas"),
UT("UT", "Utah"),
VA("VA", "Virginia"),
VT("VT", "Vermont"),
WA("WA", "Washington"),
WI("WI", "Wisconsin"),
WV("WV", "WestVirginia"),
WY("WY", "Wyoming");
private final String key;
private final String value;
State(String key, String value) {
this.key = key;
this.value = value;
}
}
I think the simplest way would be with a HashMap. Even if there was a library to convert it, it would probably use the same thing.
Map<String, String> states = new HashMap<String, String>();
states.put("Arizona", "AZ");
states.put("California", "CA");
// So on and so forth...
// Then you could create a method like
public String toStateCode(String s) {
return states.get(s);
}