Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

this is a java coding question please it is the second time that i am posting th

ID: 3770341 • Letter: T

Question

this is a java coding question please it is the second time that i am posting this question the first time the guy who answered it got it wrong!!!

hamlet.txt url http://www.buildingjavaprograms.com/code_files/3ed/ch06/hamlet.txt

words.txt url: http://www.buildingjavaprograms.com/code_files/3ed/ch13/words.txt

4) Create another program: HamletNonDictionaryWords.java. This program should find all the words in hamlet.txt that do not appear in words.txt. Shakespeare wrote Hamlet sometime between 1599 and 1602. It's considered to be in Modern English, as opposed to Middle English or Old English, but is somewhat difficult to understand for many native English speakers. Many words, such as bodkin, meaning something like knife or dagger, do not appear in most English dictionaries. What percentage of words in hamlet actually do? That's what we find out in this part of the assignment. For this you will need to either generate, or use, the ArrayLists: words (generated from words.txt), hamletWords, and hamletUniqueWords that you created in parts 2 and 3 of the assignment. For each word in hamletWords you should write code to see if it appears in words and, if it doesn't, increment a count consisting of all words that appear in hamlet.txt but not in words.txt. After going through all of the hamletWords ArrayList like this you need to report the results as follows (just an example, actual number will be different): There were 3889 words in Hamlet that do not appear in the dictionary out of a total of 3875747 words in Hamlet. Note that the first number is the value of the counter (words in hamlet.txt that were not in words.txt) and the second value is simply the total number of words in hamlet.txt. Then do the same thing for hamletUniqueWords. For each word in it check to see if it is in the words array list, if not, increment a different counter. Then report the results as follows (just an example): There were 102 distinct words in Hamlet that do not appear in the dictionary out of a total of 7644 distinct words in Hamlet. Again, the first number is the value of the new counter, but the second value is the number of unique/different/distinct words in hamlet.txt, which is however many words there are in the hamletUniqueWords ArrayList. Note that this program may take a few minutes to run due to the lengths of the lists that are being compared. Be patient. It should not take several hours to run, unless being run on an exceptionally slow machine.

Explanation / Answer

package current;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Scanner;

public class HamletNonDictionaryWords {

   public static void main(String[] theArgs) {
       Scanner hinput = null;
       Scanner winput = null;
       String str = null;
       PrintStream output = null;
       String hamletFile = "D:/ravi/Cheg/hamlet.txt";
       String wordsFile = "D:/ravi/Cheg/words.txt";
       ArrayList<String> compwords = new ArrayList<String>();
       ArrayList<String> reqwords = new ArrayList<String>();
       ArrayList<String> distcompwords = new ArrayList<String>();
       ArrayList<String> distreqwords = new ArrayList<String>();
      
       boolean filesOk = false; // Indicates if the files are accessable.
       // other variables such as arrays, should also be declared here.
       try {
           hinput = new Scanner(new File(hamletFile));
           winput = new Scanner(new File(wordsFile));
           filesOk = true;
       } catch (FileNotFoundException e) {
           System.out.println("Can't open file - " + e);
       }
       if (filesOk) {
          
           while(winput.hasNext()) {
               //System.out.println(winput.next());
               str = winput.next();
               //ignore spaces
               if(str != null && !"".equals(str.trim())) {
                   compwords.add(str.trim());
                   if(!distcompwords.contains(str.trim()))
                       distcompwords.add(str.trim());
               }
              
           }
          
           int count = 0;
           System.out.println("Size in words.txt: "+compwords.size());
           while(hinput.hasNext()) {
               //System.out.println(winput.next());
               str = hinput.next();
               count++;
               //ignore spaces
               //System.out.println(str);
               if(str != null && !"".equals(str.trim())) {
                   if(!compwords.contains(str.trim())) {
                       reqwords.add(str.trim());
                       if(!distreqwords.contains(str.trim()))
                           distreqwords.add(str.trim());
                   }

               }
              
           }
           System.out.println("There were "+reqwords.size()+" words in Hamlet that do not appear in the dictionary out of a total of "+count+" words in Hamlet");
           System.out.println("There were "+distreqwords.size()+" distinct words in Hamlet that do not appear in the dictionary out of a total of "+distreqwords.size()+" distinct words in Hamlet");
          
           //System.out.println("Total words in hamlet.txt that do not appear in words.txt : "+reqwords.size()+" out of "+count+" words");
       }
   }
  
}