Java

 

In this tutorial, we will create a simple Java program that checks if the words in a given text are spelled correctly. This tool will help you identify misspelled words by leveraging a dictionary to validate text input.

Objective

The main goal of this program is to build a spelling checker using Java. The program will take a text input, check each word against a predefined dictionary of correct words, and output the list of misspelled words (if any). This will be helpful for applications such as text editors, email clients, or other word-processing software where accurate spelling is important.

Java Code: Spelling Checker


import java.util.*;

public class SpellingChecker {

    public static void main(String[] args) {
        // Sample input text
        String text = "Ths is a sample text with smoe errors and a misspeled word.";

        // Dictionary of correctly spelled words
        Set dictionary = new HashSet<>(Arrays.asList(
            "this", "is", "a", "sample", "text", "with", "some", "errors", "and", "misspelled", "word"
        ));

        // Split the input text into words
        String[] words = text.split("\\s+");

        // List to hold misspelled words
        List misspelledWords = new ArrayList<>();

        // Check each word in the text
        for (String word : words) {
            // Remove punctuation and convert to lowercase for comparison
            String cleanWord = word.replaceAll("[^a-zA-Z]", "").toLowerCase();

            // If the word is not in the dictionary, add it to misspelledWords list
            if (!dictionary.contains(cleanWord)) {
                misspelledWords.add(word);
            }
        }

        // Output the results
        if (misspelledWords.isEmpty()) {
            System.out.println("No misspelled words found.");
        } else {
            System.out.println("Misspelled words:");
            for (String misspelled : misspelledWords) {
                System.out.println(misspelled);
            }
        }
    }
}
        

Explanation of the Program

This program uses a simple set-based approach to check for misspelled words. Here’s how it works:

  • Text Input: The program starts with a predefined string of text which contains some spelling errors.
  • Dictionary Setup: A HashSet is used to store a set of correctly spelled words. This set is used to compare against the words from the input text.
  • Word Processing: The input text is split into words using the split method, and each word is stripped of any non-alphabetic characters to make the comparison simpler.
  • Spell Check: Each word is checked against the dictionary. If a word isn’t found in the dictionary, it is considered misspelled and added to a list of misspelled words.
  • Output: The program will then output the list of misspelled words or a message saying that no errors were found.

How to Run the Program

Follow these steps to run the program:

  1. Ensure you have the Java Development Kit (JDK) installed on your machine. If not, you can download it from the official Java website.
  2. Create a new Java file (e.g., SpellingChecker.java) and copy the provided code into this file.
  3. Open a terminal or command prompt, navigate to the directory where the Java file is saved, and compile the code using the following command:
    javac SpellingChecker.java
  4. Once compiled, run the program using:
    java SpellingChecker
  5. Observe the output, which will either list any misspelled words or indicate that the text is free of spelling mistakes.
© 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 :)