Java

 

Introduction

Scrabble is a popular word game that assigns points based on letter tiles used in the words formed. The aim of this article is to help you understand how to calculate the score of a Scrabble word using Java programming language.

Objective

The objective of this program is to calculate the total score of a given Scrabble word based on the values of individual letters. In Scrabble, each letter has a specific point value. The program will accept a word as input and output the total score of that word by summing up the values of its individual letters.

Java Program to Calculate Scrabble Word Score

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

public class ScrabbleScoreCalculator {

    public static void main(String[] args) {
        // Create a map to hold letter values
        Map<Character, Integer> letterScores = new HashMap<>();
        
        // Assign score values to each letter
        letterScores.put('A', 1);
        letterScores.put('B', 3);
        letterScores.put('C', 3);
        letterScores.put('D', 2);
        letterScores.put('E', 1);
        letterScores.put('F', 4);
        letterScores.put('G', 2);
        letterScores.put('H', 4);
        letterScores.put('I', 1);
        letterScores.put('J', 8);
        letterScores.put('K', 5);
        letterScores.put('L', 1);
        letterScores.put('M', 3);
        letterScores.put('N', 1);
        letterScores.put('O', 1);
        letterScores.put('P', 3);
        letterScores.put('Q', 10);
        letterScores.put('R', 1);
        letterScores.put('S', 1);
        letterScores.put('T', 1);
        letterScores.put('U', 1);
        letterScores.put('V', 4);
        letterScores.put('W', 4);
        letterScores.put('X', 8);
        letterScores.put('Y', 4);
        letterScores.put('Z', 10);
        
        // Example Scrabble word
        String word = "JAVA";
        int score = calculateScore(word.toUpperCase(), letterScores);
        
        System.out.println("The score of the word \"" + word + "\" is: " + score);
    }

    // Method to calculate the total score of a word
    public static int calculateScore(String word, Map<Character, Integer> letterScores) {
        int totalScore = 0;
        
        // Loop through each letter in the word and add the score to totalScore
        for (int i = 0; i < word.length(); i++) {
            char letter = word.charAt(i);
            if (letterScores.containsKey(letter)) {
                totalScore += letterScores.get(letter);
            }
        }
        
        return totalScore;
    }
}

Explanation of Program Structure

This program is structured as follows:

  • Letter Scores Map: A HashMap is used to map each letter in the alphabet to its corresponding Scrabble score.
  • Input Word: The word to calculate the score for is hardcoded in the program (you can modify this to accept user input). In this case, the word “JAVA” is used as an example.
  • calculateScore Method: This method takes the word and the map of letter scores as inputs, loops through the word’s characters, and sums their corresponding scores. The result is then returned.
  • Output: The program prints the score of the word to the console.

How to Run the Program

To run this program on your system, follow these steps:

  1. Make sure you have the Java Development Kit (JDK) installed on your machine. If not, download and install it from the official Oracle website.
  2. Create a new file named ScrabbleScoreCalculator.java and paste the above code into it.
  3. Open a terminal or command prompt and navigate to the directory where the file is saved.
  4. Compile the program by running the following command:
    javac ScrabbleScoreCalculator.java
  5. Run the compiled program using this command:
    java ScrabbleScoreCalculator
  6. The program will output the score of the Scrabble word.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)