I have a problem which I can't solve. I want to solve Alphametics(e.g. SEND + MORE = MONEY --> O=0,M=1,Y=2,E=5,N=6,D=7,R=8,S=9) So I tried to make a Equation out of that like this: 1000*S+100*E+10*N+D + 1000*M+100*O+10*R+E = 10000*M+1000*O+100*N+10*E+Y So I tried to use a Hashmap, to collect this Data(this is just for the left side of the equation):
Scanner s = new Scanner(System.in);
int HowMuchWords = s.nextInt();
String[] Words = new String[HowMuchWords];
for(int i = 0; i<Words.length;i++){
Words[i] = s.next().toUpperCase();
}
HashMap<Character,Integer> Letters = new HashMap<Character, Integer>();
for(int i = 0;i<Words.length;i++) {
char[] LettersWord = Words[i].toCharArray();
for (int j = 0; j < LettersWord.length; j++) {
Letters.put(LettersWord[j],Letters.get(LettersWord[j])+(int) Math.pow(10, LettersWord.length - 1 - j));
}
But I have problems with the Letters.get command. Because I want to add a number to null, the Value is still null. So I want to set the default value of every value in the Hashmap to 0. Is there a possibility to do that?