Storing constants in Java -
i have table of chars , each char int value affected. (i use table calculate barcode check digit).
i want store table constants, best way in java please? (enum, hashmap...)
any appreciated.
if mapping between letters , codes in own answer, not need table. can simple translation method:
public static int lettertocode(char letter) { if (letter >= '0' && letter <= '9') { return letter - '0'; } else if (letter >= 'a' && letter <= 'z') { return letter - 'a' + 10; } else { throw new illegalargumentexception("invalid letter: " + letter); } } public static char codetoletter(int code) { if (code >= 0 && code <= 9) { return (char) ('0' + code); } else if (code >= 10 && code <= 35) { return (char) ('a' + code - 10); } else { throw new illegalargumentexception("invalid code: " + code); } }
this not perform worse looking code or letter in map.
Comments
Post a Comment