I have a table of chars and for each char an int value is affected. (I will use this table to calculate a barcode check digit).
I want to store this table as constants, what is the best way to do it in java please? (Enum, Hashmap...)
Any help will be appreciated.
Best How To :
If the mapping between letters and codes is as in your own answer, then you do not need a table. You can do this with a 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 will not perform worse than looking up the code or letter in a map.