In: Computer Science
(JAVA)
We will write a program to check the spelling of the word entered by user, using data from a dictionary, which is text file. The program will ask user to enter a word for spell check, and then check in the text file (dictionary.txt) if the word exists. (The dictionary.txt file is provided) If the word exists in text file, the output will be “Word is correctly spelled”. If the word doesn’t exist in the text file, program will write the message “Word doesn’t exist in the Dictionary” and will request user to enter “yes” or “no” to save the word in another text file. If user wants to write the word in a text file, user will enter “yes” (This text file can be named anything, for example “PersonalDictionary.txt”). Note: at this step, you need to write the word in the text file and display the message “****** is saved successfully in the file” (Asterisks denote the word entered by the user). If user doesn’t want to write/save the word in the file, user will enter “no” and the program will display a message “****** is not stored in the file” (Where asterisks denote the word). After this, program will ask the user to enter another word or enter some key (for example: -1) to exit the program. The program will use “dictionary.txt” file to read the list of words to be compared with. The program will create and write into a file (you can name it anything, for example “updateDictionary.txt”), the list of words that don’t match.
1. (60 points) Program runs successfully
a. (10 points) Inputs word from the user using a Scanner object.
b. (10 points) Reads the dictionary text file and save the data in a variable
c. (10 points) Repeats until user exit the program (Using Sentinel value to stop)
d. (15 points) Writes the word into a new personal dictionary file
e. (15 points) Outputs the final summary correctly for spelling check
2. (40 points) Program contents follow readability standards including:
a. (10 points) Correct use of arrayLists or any other data type
b. (10 points) Successfully write and use a method outside of the main method
c. (10 points) Correct variable types with Useful, Camel-case identifiers and proper tabbing.
d. (10 points) Document your code with at least 5 helpful comments
i. Put comments above the lines of code they are documenting, not to the right
I cannot attach the dictionary.txt file i appologize
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class DictionaryCheck {
public static void main(String[] args) {
String filename = "dictionary.txt";
String personalFile = "personalDictionary.txt";
Dictionary dictionary = new Dictionary(filename);
PersonalDictionary personalDictionary = new PersonalDictionary(personalFile);
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("Enter Word to check Word in dictionary or -1 to exit");
String word = sc.next();
if (word.equals("-1")) {
break;
}
if (dictionary.isWordFound(word)) {
System.out.println("Word is correctly spelled");
} else {
System.out.println("Word doesn’t exist in the Dictionary");
System.out.println("Would you like to save word in another file?\n Enter YES/NO");
String option = sc.next();
if (option.equalsIgnoreCase("YES")) {
if(!personalDictionary.isWordFound(word))
personalDictionary.addWord(word);
System.out.println(word + " is saved successfully in the file");
} else {
System.out.println(word + " is not stored in the file");
}
}
}
sc.close();
}
}
class Dictionary {
ArrayList<String> dict;
/**
* Initialization of Dictionary Class and Calling Read method to extract all words in dictionary
* @param filename
*/
public Dictionary(String filename) {
dict = new ArrayList<>();
readWords(filename);
}
/**
* Check for the word exists in dictionary or not
* @param word
* @return true if found else false
*/
public boolean isWordFound(String word) {
for (String wordInDict : dict) {
if (word.trim().toLowerCase().equalsIgnoreCase(wordInDict)) {
return true;
}
}
return false;
}
/**
* Read each word in the dictionary.txt and store in ArrayList
* @param filename
*/
protected void readWords(String filename) {
Scanner sc = null;
try {
sc = new Scanner(new File(filename));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
while (sc.hasNext()) {
dict.add(sc.next().trim());
}
if (sc != null)
sc.close();
}
}
/**
* Subclass Of Dictionary having additional method to add new words
*
*/
class PersonalDictionary extends Dictionary {
String filename;
/**
* @param filename
*/
public PersonalDictionary(String filename) {
super(filename);
this.filename = filename;
}
/**
* Adding new words to person dictionary i.e list as well as file.
* @param word
*/
public void addWord(String word) {
dict.add(word);
File file = new File(filename);
try {
FileWriter fw = new FileWriter(file,true);
fw.append("\n");
fw.append(word.trim());
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Add "dictionary.txt" and "personalDictionary.txt" at the root of project/dir before running thr program. Also add few words in dictionary.txt to check spelling after running.
Output Sample: