Question

Read a "dictionary" file and store the listed words in an array (not an arraylist) Analyze...

Read a "dictionary" file and store the listed words in an array (not an arraylist)

Analyze a given text file "file.txt" and search for every word in "file.txt" in the "dictionary.txt" file using a binary search algorithm. (You may not use the binarySearch method in arrays class)

Print words not found in the dictionary as potentially incorrect; count the number of incorrectly spelled/not located words, count the number of correctly spelled words, and count the total number of words. Output those counters alongside the total time to run the program.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE

Main.java   

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.*;

class Main {

    public static void main(String[] args) throws java.lang.Exception {

        long startTime = System.nanoTime();
        //reading the file
        //provide the path where Dict.txt exist
        File dict = new File("C:\\Users\\YASH\\IdeaProjects\\Hiring\\src\\Dict.txt");
        BufferedReader br = new BufferedReader(new FileReader(dict));

        StringBuilder s = new StringBuilder();
        String sx;
        while ((sx = br.readLine()) != null) {
            s.append(sx).append(" ");
        }
        //stored in array
        String[] T = s.toString().split(" ");

        //provide the path where  File.txt exist
        File file = new File("C:\\Users\\YASH\\IdeaProjects\\Hiring\\src\\File.txt");
        BufferedReader bx = new BufferedReader(new FileReader(file));

        String sz;
        s = new StringBuilder();
        while ((sz = bx.readLine()) != null) {
            s.append(sz).append(" ");
        }

        //file words stored in array
        String[] F = s.toString().split(" ");

        //sorting the words in dictionary
        Arrays.sort(T);

        int incorrect = 0;
        int correct = 0;
        for (String value : F) {
            if (binarySearch(T, value) == -1) {
                incorrect++;
                System.out.println(" potentially incorrect ");
            } else {
                correct++;
                System.out.println(" " + value + " ");
            }
        }
        long endTime = System.nanoTime();
        System.out.println(" incorrect = " + incorrect);
        System.out.println(" correct = " + correct);

        double time = (endTime-startTime);

        //time in milli second
        System.out.println(" time = " + time/1000000 ) ;
    }

    //binary search to search the words
    private static int binarySearch(String[] A, String x) {
        int left = 0, right = A.length - 1;
        while (left <= right) {
            int mid = (right + left) / 2;
            int res = x.compareTo(A[mid]);
            if (res == 0)
                return mid;
            if (res > 0)
                left = mid + 1;
            else
                right = mid - 1;
        }
        return -1;
    }
}

Output

Words in Dict.txt

Apple
Axle
Box
Boy
Cat
Dog

Words in File.txt

Boy
Axle
Bush

Output

Boy
Axle
potentially incorrect
incorrect = 1
correct = 2
time = 1.1894

Add a comment
Know the answer?
Add Answer to:
Read a "dictionary" file and store the listed words in an array (not an arraylist) Analyze...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA Write a program which will read a text file into an ArrayList of Strings. Note...

    JAVA Write a program which will read a text file into an ArrayList of Strings. Note that the given data file (i.e., “sortedStrings.txt”) contains the words already sorted for your convenience. • Read a search key word (i.e., string) from the keyboard and use sequential and binary searches to check to see if the string is present as the instance of ArraryList. • Refer to “SearchInt.java” (given in “SearchString.zip”) and the following UML diagram for the details of required program...

  • Array Processing - Dictionary Program NOTE: Review the parallel array example from class Another method of...

    Array Processing - Dictionary Program NOTE: Review the parallel array example from class Another method of "Sequentially Searching an Array" is covered in the book in Chapter 8. Using Notepad, write psuedocode ONLY for the following situation. Create and load an array with the following 7 values. Add one more word (of your own choosing) for a total of 8 words. biff comely fez mottle peruke bedraggled quisling Create a second array (parallel array). To hold the defintions to these...

  • This lab will combine reading data from a file and searching the array to find a...

    This lab will combine reading data from a file and searching the array to find a specific value. Assignment Write a program that reads in a file full of strings into an array, and prompts the user for a string to find in the array. The program should loop until a sentinel value (such as -1) is entered. After looping in main() for the input, write a search function, with the following prototype: int findWord(string [], int, string); with arguments...

  • In this assignment you will implement the second version of your spell checker. Using the randomi...

    In this assignment you will implement the second version of your spell checker. Using the randomized dictionary (random_dictionary.txt) given, read in the dictionary into an array of 26 Binary Search Trees (BST) , one for each letter of the alphabet. Thus the first BST would contain only those words starting with letter 'a', while the last would contain only those words starting with letter 'z'. Then, when you read in the book (oliver.txt), you examine the first character of each...

  • In this assignment you will implement the second version of your spell checker. Using the randomized...

    In this assignment you will implement the second version of your spell checker. Using the randomized dictionary (random_dictionary.txt) given, read in the dictionary into an array of 26 Binary Search Trees (BST) , one for each letter of the alphabet. Thus the first BST would contain only those words starting with letter 'a', while the last would contain only those words starting with letter 'z'. Then, when you read in the book (oliver.txt), you examine the first character of each...

  • Having issues using binary search on a pointer array. 1. Write 1000 random ints to file...

    Having issues using binary search on a pointer array. 1. Write 1000 random ints to file 2. Binary Search to find if number exists in element from file. int main() { ArrayActions action;   int count; int userInput; int num = 1000; int array[num]; ofstream myFile ("/Users/chan/Desktop/LANEY_CIS27/Assignemtn2_CIS27/Assignemtn2_CIS27/File.txt");   //Set srand with time to generate unique random numbers srand((unsigned)time(0));   //Format random num up to 999 for(count = 0; count < num; count++) { array[count] = rand() % 1000; }          //Condition...

  • I cant get this python program to read all the unique words in a file. It...

    I cant get this python program to read all the unique words in a file. It can only read the number of the unique words in a line. import re import string from collections import Counter # opens user inputted filename ".txt" and (w+) makes new and writes def main(): textname = input("Enter the file to search: ") fh = open(textname, 'r', encoding='utf-8' ) linecount = 0 wordcount = 0 count = {} print("Sumary of the", fh) for line in...

  • CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the fil...

    CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the file is completely read, write the words and the number of occurrences to a text file. The output should be the words in ALPHABETICAL order along with the number of times they occur and the number of syllables. Then write the following statistics to...

  • I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt...

    I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt Project#3 is an extension of the concepts and tasks of Lab#3. You will again read the dictionary file and resize the array as needed to store the words. Project#3 will require you to update a frequency counter of word lengths every time a word is read from the dictionary into the wordList. When your program is finished this histogram array will contain the following:...

  • I need help in C++ implementing binary search tree. I have the .h file for the...

    I need help in C++ implementing binary search tree. I have the .h file for the binary search tree class. I have 4 classic texts, and 2 different dictionaries. Classic Texts: Alice In Wonderland.txt A Tale of Two Cities.txt Pride And Prejudice.txt War and Peace.txt 2 different dictionaries: Dictionary.txt Dictionary-brit.txt The data structures from the standard template library can not be used.The main program should open the text file, read in the words, remove the punctuation and change all the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT