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 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...
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...
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....
Write a program that asks the user for an angle, entered in radians. The program should...
Write a program that asks the user for an angle, entered in radians. The program should then display the sine, cosine, and tangent of the angle. (Use the sin, cos, and tan library functions to determine these values.) The output should be displayed in fixed-point notation, rounded to four decimal places of precision Take your previous Angle Calculator program and modify it to do a table of trig values. The columns will be: Degrees, Sine, Cosine, Tangent,. And the rows...
using java language "Data Structures" I have to write program which stores string entered by user...
using java language "Data Structures" I have to write program which stores string entered by user into cursor array implementation here is the code public static void main(String[] args) { CursorArray sorted =new CursorArray();//the strings must added here how can i store them                  String []inputs = new String[50];                   for (int i=0; i< 50; i++) {            System.out.println("Enter the words you want to sort and use exit to stop");...
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
*Please write code in C++* Write a program to verify the validity of the user entered...
*Please write code in C++* Write a program to verify the validity of the user entered email address.   if email is valid : output the stating that given email is valid. ex: "The email [email protected] is valid" else : output the statement that the email is invalid and list all the violations ex:  "The email sarahwinchester.com is invalid" * @ symbol * Missing Domain name The program should keep validating emails until user enter 'q' Upload your source code. ex: main.cpp
IN JAVA write a program that asks a user for a maximum of 20 user-inputted elements...
IN JAVA write a program that asks a user for a maximum of 20 user-inputted elements and create an array. Then, write a Merge Sort function with recursion (in the main) that takes the user inputted elements in the array, sorts them, and prints them back.
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user to enter the name of the month he/she was born in (example: September). b) Prompt the user to enter his/her weight in pounds (example: 145.75). c) Prompt the user to enter his/her height in feet (example: 6.5). d) Display (print) a line of message on the screen that reads as follows: You were born in the month of September and weigh 145.75 lbs. and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT