Question

In: Computer Science

Java Programming: spellcheck Write code to read in a dictionary from the current directory called "dict.txt"...

Java Programming: spellcheck

Write code to read in a dictionary from the current directory called "dict.txt" (you can find this in the current directory/data)

Add each word to a hash map after splitting words and throwing away punctuation (see the demo).

open a file called "spell.txt" to check in the current directory.

Print out every word in spell.txt that is NOT in the dictionary.

Example:

if spell.txt contains:

hello, this is a test. 2152189u5
Misspelled! cApitalized

The output should be:
2152189u5
Misspelled
cApitalized

Solutions

Expert Solution

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map.Entry;

public class SpellCheck {

    private static HashMap<String, String> readWords(String fileName) throws IOException {

        var lines = Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
        var itr = lines.iterator();
        var map = new HashMap<String, String>();

        while (itr.hasNext()) {
            var words = itr.next().split("\\W");
            for (String word : words) {
                if (!map.containsKey(word))
                    map.put(word.toLowerCase(), word);
            }
        }
        
        return map;
    }

    public static void main(String[] args) throws IOException {

        var dictWords = readWords("dict.txt");
        var fileWords = readWords("spell.txt");

        for (Entry<String, String> word : fileWords.entrySet()) {
            if(!dictWords.containsKey(word.getKey()))
                System.out.println(word.getValue());
        }
    }
}
Contect of dict.txt
hello, this is a sample test dictionary.
Content of spell.txt
hello, this is a test. 2152189u5
Misspelled! cApitalized
Output
cApitalized
2152189u5
Misspelled

Code with sample input output.


Related Solutions

Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate the key 'class_name' with the value 'MAT123', and associate the key 'sect_num' with '211_145'
Using JAVA The following code is able to read integers from a file that is called...
Using JAVA The following code is able to read integers from a file that is called "start.ppm" onto a 3d array called "startImage". Implement the code by being able to read from another file (make up any file name) and save the data onto another 3d array lets say you call that array "finalImage". The purpose of this will be to add both arrays and then get the average Save the average onto a separte 3darray,lets say you call it...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and then increments the number of seconds. The counter will start at 00:00 and each time the number of seconds reaches 60, minutes will be incremented. You will not need to implement hours or a sleep function, but if minutes or seconds is less than 10, make sure a leading zero is put to the left of the number. For example, 60 seconds:...
Why is java programming better than bash scripting. Why should programmers write in java code ?...
Why is java programming better than bash scripting. Why should programmers write in java code ? Come up with situations where java is a better option.
Code in C++ programming language description about read and write data to memory example.
Code in C++ programming language description about read and write data to memory example.
JAVA CODE // Write a method called example that will do several things. // It has...
JAVA CODE // Write a method called example that will do several things. // It has two integer array parameters of unknown varying lengths. // It returns an integer array that contains each of the first array values // divided by two and each of the second array values added to the number 100. // It prints each value of the first array and then prints that value // divided by two on a separate line. It then prints each...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
In STS, create Java Project, called java_generics_yourNameLastname that; (Under source directory called, src, create a package...
In STS, create Java Project, called java_generics_yourNameLastname that; (Under source directory called, src, create a package csci3444.generics and put all code in it) Create a generic interface called “MyGenInterface” that takes 2 generic types K,V with below methods; public K getKey(); public V getValue(); Create a generic class called “MyGenClass” that implements “MyGenInterface” interface. has attributes “K key” and “V value” that can be inherited has a constructor that takes “K _key”, “V _value” inputs and initializes “key”, “value” attributes...
JAVA CODE --- from the book, java programming (7th edition) chapter 7 carly's question I am...
JAVA CODE --- from the book, java programming (7th edition) chapter 7 carly's question I am getting a illegal expression error and don't know how to fix it, also may be a few other error please help CODE BELOW import java.util.Scanner; public class Event { public static double pricePerGuestHigh = 35.00; public static double pricePerGuestLow = 32.00; public static final int LARGE_EVENT_MAX = 50; public String phnum=""; public String eventNumber=""; private int guests; private double pricePerEvent; public void setPhoneNumber() {...
Write the code in C++. Write a program to implement Employee Directory. The main purpose of...
Write the code in C++. Write a program to implement Employee Directory. The main purpose of the class is to get the data, store it into a file, perform some operations and display data. For the purpose mentioned above, you should write a template class Directory for storing the data empID(template), empFirstName(string), empLastName(string), empContactNumber(string) and empAddress(string) of each Employee in a file EmployeeDirectory.txt. 1. Write a function Add to write the above mentioned contact details of the employee into EmployeeDirectory.txt....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT