Question

In: Computer Science

(JAVA) We will write a program to check the spelling of the word entered by user,...

(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

Solutions

Expert Solution

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:


Related Solutions

Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
This is Java In this problem we will write a program that asks the user to...
This is Java In this problem we will write a program that asks the user to enter a) The user's first name and b) a series of integers separated by commas. The integers may not include decimal points nor commas. The full string of numbers and commas will not include spaces either, just digits and commas. An example of valid input string is:        7,9,10,2,18,6 The string must be input by the user all at once; do not use a loop...
Write a Java program that directs the user to enter a single word (of at least...
Write a Java program that directs the user to enter a single word (of at least four characters in length) as input and then prints the word out in reverse in the pattern shown below (please note that the spaces added between each letter are important and should be part of the program you write): <Sample Output Enter a word (at least four characters in length): cat Word must be at least four characters in length, please try again. Enter...
Java Project Requirements: 1.Write a Java program that plays a word game with a user. The...
Java Project Requirements: 1.Write a Java program that plays a word game with a user. The program asks the user questions and then creates a paragraph using the user’s answers. 2.The program must perform the following: a.Uses a Scanner object to ask the user: (The program asks for no other information) i.Full Name (First and Last name only) - stores this Full Name in one String object variable. ii.Age – must be read in as an int. iii.Profession or expected...
JAVA Write a program that checks the spelling of words in a document. This program uses...
JAVA Write a program that checks the spelling of words in a document. This program uses two text files: A dictionary file containing all known correctly spelled words, and A document to be spell-checked against the dictionary. As the document to be spell checked is read, each of its words is checked against the dictionary words. The program determines whether each word is misspelled. Misspelled words are recorded. is spelled correctly. Correctly spelled words are recorded and their frequency counted....
2. Write a Java program to read a string (a password)from the user and then   check...
2. Write a Java program to read a string (a password)from the user and then   check that the password conforms to the corporate password policy.   The policy is:   1) the password must be at least 8 characters   2) the password must contain at least two upper case letters   3) the password must contain at least one digit   4) the password cannot begin with a digit   Use a for loop to step through the string.   Output “Password OK” if the password...
Write a C++ program that finds the minimum number entered by the user .The user is...
Write a C++ program that finds the minimum number entered by the user .The user is allowed to enter negative numbers 0, or positive numbers. The program should be controlled with a loop statement. Break statements is not allowed to break from loop. After each number inputted by the user, The program will prompt user if they need to enter another number. User should enter either Y for yes or N for no. Make Sure the user enters either Y...
Write a Java application that prompts the user for an age. If the age entered is...
Write a Java application that prompts the user for an age. If the age entered is greater or equal to 65, display the statement "Age is greater than or equal to 65"; otherwise display the message "Age is less than 65". If the age entered is less than 18; display the statement "This person is a minor"; otherwise display the message "This person can legally vote. Do not create a class for this application. The code can be created in...
Instructions: Create a Java program that reads a string entered by the user and then determines...
Instructions: Create a Java program that reads a string entered by the user and then determines and prints how many of each lowercase vowel (a, e, i, o, and u) appear in the entire string. Have a separate counter for each vowel. Also, count and print the number of non-vowel characters. Example: User enters: "This house is beautiful." a: 1 e: 2 i: 3 o: 1 u: 2 non-vowel:10
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT