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
splitmethod, 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:
- Ensure you have the Java Development Kit (JDK) installed on your machine. If not, you can download it from the official Java website.
- Create a new Java file (e.g.,
SpellingChecker.java) and copy the provided code into this file. - 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 - Once compiled, run the program using:
java SpellingChecker - Observe the output, which will either list any misspelled words or indicate that the text is free of spelling mistakes.


Aw, this was a very nice post. In idea I want to put in writing like this additionally – taking time and actual effort to make a very good article… however what can I say… I procrastinate alot and in no way appear to get one thing done.
Undeniably believe that which you stated. Your favorite reason appeared to be on the net the easiest thing to be aware of. I say to you, I definitely get irked while people think about worries that they just don’t know about. You managed to hit the nail upon the top as well as defined out the whole thing without having side effect , people could take a signal. Will probably be back to get more. Thanks