Scrabble score calculator in Java Please review my code as I'm wondering if there is a better way. Here is the question I'm trying to solve:


  Scrabble Score
  
  Given a word, compute the scrabble score for that word.
  
  Letter Values
  
  You'll need these:

```plain
Letter                           Value
A, E, I, O, U, L, N, R, S, T       1
D, G                               2
B, C, M, P                         3
F, H, V, W, Y                      4
K                                  5
J, X                               8
Q, Z                               10
```

  
  Examples
  
  "cabbage" should be scored as worth 14 points:
  
  
  3 points for C
  1 point for A, twice
  3 points for B, twice
  2 points for G
  1 point for E
  
  
  And to total:
  
  
  3 + 2*1 + 2*3 + 2 + 1
  = 3 + 2 + 6 + 3
  = 5 + 9
  = 14
  
  
  Extensions
  
  
  You can play a double or a triple letter.
  You can play a double or a triple word.
  


Here's my answer:

import java.util.HashMap;
import java.util.Map;

class Scrabble {

    private String word;

    Scrabble(String word) {
        this.word = word;
    }

    int getScore() {

        Map&lt;Character, Integer&gt; lettersMap = new HashMap&lt;&gt;();
        String lettersCap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        for (int i = 0; i &lt; lettersCap.length(); i++) {
            if (lettersCap.charAt(i) == 'A' || lettersCap.charAt(i) == 'E' ||
                    lettersCap.charAt(i) == 'I' || lettersCap.charAt(i) == 'O' ||
                    lettersCap.charAt(i) == 'O' || lettersCap.charAt(i) == 'U' ||
                    lettersCap.charAt(i) == 'L' || lettersCap.charAt(i) == 'N' ||
                    lettersCap.charAt(i) == 'R' || lettersCap.charAt(i) == 'S' ||
                    lettersCap.charAt(i) == 'T') {

                lettersMap.put(lettersCap.charAt(i), 1);
                lettersMap.put(lettersCap.toLowerCase().charAt(i), 1);
            }

            if (lettersCap.charAt(i) == 'D' || lettersCap.charAt(i) == 'G') {
                lettersMap.put(lettersCap.charAt(i), 2);
                lettersMap.put(lettersCap.toLowerCase().charAt(i), 2);
            }

            if (lettersCap.charAt(i) == 'B' || lettersCap.charAt(i) == 'C' ||
                    lettersCap.charAt(i) == 'M' || lettersCap.charAt(i) == 'P') {
                lettersMap.put(lettersCap.charAt(i), 3);
                lettersMap.put(lettersCap.toLowerCase().charAt(i), 3);
            }

            if (lettersCap.charAt(i) == 'F' || lettersCap.charAt(i) == 'H' ||
                    lettersCap.charAt(i) == 'V' || lettersCap.charAt(i) == 'W' ||
                    lettersCap.charAt(i) == 'Y') {
                lettersMap.put(lettersCap.charAt(i), 4);
                lettersMap.put(lettersCap.toLowerCase().charAt(i), 4);
            }

            if (lettersCap.charAt(i) == 'K') {
                lettersMap.put(lettersCap.charAt(i), 5);
                lettersMap.put(lettersCap.toLowerCase().charAt(i), 5);
            }

            if (lettersCap.charAt(i) == 'J' || lettersCap.charAt(i) == 'X') {
                lettersMap.put(lettersCap.charAt(i), 8);
                lettersMap.put(lettersCap.toLowerCase().charAt(i), 8);
            }

            if (lettersCap.charAt(i) == 'Q' || lettersCap.charAt(i) == 'Z') {
                lettersMap.put(lettersCap.charAt(i), 10);
                lettersMap.put(lettersCap.toLowerCase().charAt(i), 10);
            }

        }

        int totalValue = 0;

        for (int j = 0; j &lt; word.length(); j++) {

            totalValue += lettersMap.get(word.charAt(j));
        }

        return totalValue;
    }

}
First of all, you should probably create lettersMap from your constructor. You don't need to create it again every time you want to get score for a word.

There are various ways to make the code shorter. The simplest method is to define the scores as an array, in the same order as the letters:

int[] letterScore = {1, 3, 3, 2 ... 4, 10}; 


Then your whole for-cycle gets reduced to

for (int i = 0; i &lt; lettersCap.length(); i++) {
    lettersMap.put(lettersCap.charAt(i), letterScore[i]);
    lettersMap.put(lettersCap.toLowerCase().charAt(i), letterScore[i]);
}


However, it will also make the code less readable.

You could also define every part separately:

char[] score1letters = {'A', 'E', 'I', 'O', … 'T'};
char[] score2letters = {'D', 'G'};
...
char[] score10letters = {'Q', 'Z'}


and for every such array:

for (int i = 0; i &lt; score1letters.length; i++) {
    lettersMap.put(lettersCap.charAt(i), 1);
    lettersMap.put(lettersCap.toLowerCase().charAt(i), 1);
}


To be honest, the most readable version will probably be:

private static final Map&lt;Character, Integer&gt; lettersMap;
static {
    lettersMap = new HashMap&lt;Character, Integer&gt;();
    lettersMap.put('A', 1);
    lettersMap.put('B', 3);
    lettersMap.put('C', 3);
    lettersMap.put('D', 2);
    ...
    lettersMap.put('Y', 4);
    lettersMap.put('Z', 10);
}


To put lowercase chars, you can either get all the keys at the end and copy their values with lowercased key, or, you can leave only uppercase letters there and instead convert the word to uppercase before calculating its score.