Question

In: Computer Science

you need to submit the following files: Main.java Additionally, you need to download file ‘letter_count.csv’, that...

you need to submit the following files:

  • Main.java

Additionally, you need to download file ‘letter_count.csv’, that contains counts for characters in a text (see submission folder on iCollege) and put it into the root of related Eclipse project folder. To view your project folder through system file explorer, right-click on ‘src’ folder in the Eclipse project explorer and choose ‘Show In->System Explorer’.

Consider the following Java code, that reads a .csv file:

BufferedReader csvReader = new BufferedReader(new FileReader("letter_count.csv"));

String currentRow = "";

while ((currentRow = csvReader.readLine()) != null) {

String[] data = currentRow.split(",");

// Use data

// ...

//

}

csvReader.close();

Update the following code:

  • Add necessary try-catch blocks, so that potential IOExceptions are handled;

  • Print only letters with count > 500 000 to log

    • This is the content of

      letter_count.csv:
      Letter Frequency
      "A" 234473121
      "B" 4762938
      "C" 8982417
      "D" 10805580
      "E" 37907119
      "F" 7486889
      "G" 111111
      "H" 18058207
      "I" 21820970
      "J" 474021
      "K" 2222222
      "L" 11730498
      "M" 7391366
      "N" 21402466
      "O" 23215532
      "P" 5719422
      "Q" 333333
      "R" 17897352
      "S" 4444444
      "T" 28691274
      "U" 8022379
      "V" 2835696
      "W" 6505294
      "X" 562732
      "Y" 5910495
      "Z" 55555

Solutions

Expert Solution

Code

//packages required to read the csv file are imported

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Main {

   public static void main(String[] args) {
       BufferedReader csvReader = null;
       try {
           //reading CSV file, as it is in root otherwise absolute or complete path to the file had to be given
           csvReader = new BufferedReader(new FileReader("letter_count.csv"));
             
       } catch (FileNotFoundException e1) {
           //FileNotFound Exception handled through try catch
           System.out.println("Exception occured in Main.class and reason is "
                   + e1.getMessage());
       }

       String currentRow = "";

       try {
           while ((currentRow = csvReader.readLine()) != null) {
               String[] data = currentRow.split(",");
               if (!data[1].equalsIgnoreCase("Frequency")) {
                   try{
                   if (Long.parseLong(data[1]) > 500000)//converting the string to long for checking if value is >500000
                   {
                       System.out.println("Letter"+data[0]+" Count"+data[1]);//printing only the letters with count > 500 000
                   }
                   }
                   catch (NumberFormatException e) {
                      
                       //if the count is not a Number then an exception is thrown
                       //written inside the while to avoid abrupt termination of the program
                       System.out.println("Exception occured in Main.class and reason is "
                               + e.getMessage());
                   }

               }
           }
       } catch (IOException e) {
           //handling IOExceptions while reading the file
           System.out.println("Exception occured in Main.class and reason is "
                   + e.getMessage());
       }

       try {
           csvReader.close();
       } catch (IOException e) {
           //handling IOExceptions while reading the file
           System.out.println("Exception occured in Main.class and reason is "
                   + e.getMessage());
       }

   }//end of main()

}//end of Main

//packages required to read the csv file are imported

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Main {

        public static void main(String[] args) {
                BufferedReader csvReader = null;
                try {
                        //reading CSV file, as it is in root otherwise absolute or complete path to the file had to be given
                        csvReader = new BufferedReader(new FileReader("letter_count.csv")); 
                         
                } catch (FileNotFoundException e1) {
                        //FileNotFound Exception handled through try catch
                        System.out.println("Exception occured in Main.class and reason is "
                                        + e1.getMessage());
                }

                String currentRow = "";

                try {
                        while ((currentRow = csvReader.readLine()) != null) {
                                String[] data = currentRow.split(",");
                                if (!data[1].equalsIgnoreCase("Frequency")) { 
                                        try{ 
                                        if (Long.parseLong(data[1]) > 500000)//converting the string to long for checking if value is >500000
                                        {
                                                System.out.println("Letter"+data[0]+" Count"+data[1]);//printing only the letters with count > 500 000
                                        }
                                        }
                                        catch (NumberFormatException e) {
                                                
                                                //if the count is not a Number then an exception is thrown
                                                //written inside the while to avoid abrupt termination of the program 
                                                System.out.println("Exception occured in Main.class and reason is "
                                                                + e.getMessage());
                                        }

                                }
                        }
                } catch (IOException e) {
                        //handling IOExceptions while reading the file
                        System.out.println("Exception occured in Main.class and reason is "
                                        + e.getMessage());
                } 

                try {
                        csvReader.close();
                } catch (IOException e) {
                        //handling IOExceptions while reading the file
                        System.out.println("Exception occured in Main.class and reason is "
                                        + e.getMessage());
                }

        }//end of main()

}//end of Main

Folder Structure--- The CSV file to keep in root folder as said

Output Window-

Letters_Count File Snippet--


Related Solutions

Go to the Files section and download the AFE_Test file from the Datasets folder. We are...
Go to the Files section and download the AFE_Test file from the Datasets folder. We are interested in a one­tail test described in the following fashion: Ho: u < or = to 200 CFM; H1: u > 200 CFM. At 5% significance level, we can reject the null hypothesis given the sample information in AFE_Test1. we can reject the null hypothesis given the sample information in AFE_Test2. we cannot reject the null hypothesis. we can reject the null hypothesis given...
In this programming assignment, you will implement a SimpleWebGet program for non- interactive download of files...
In this programming assignment, you will implement a SimpleWebGet program for non- interactive download of files from the Internet. This program is very similar to wget utility in Unix/Linux environment.The synopsis of SimpleWebGet is: java SimpleWebGet URL. The URL could be either a valid link on the Internet, e.g., www.asu.edu/index.html, or gaia.cs.umass.edu/wireshark-labs/alice.txt or an invalid link, e.g., www.asu.edu/inde.html. ww.asu.edu/inde.html. The output of SimpleWebGet for valid links should be the same as wget utility in Linux, except the progress line highlighted...
There are two programming questions for you to do. Please submit two.s or .asm files. You...
There are two programming questions for you to do. Please submit two.s or .asm files. You can use the usual settings as the previous homework. But if you want to use pseudo-instructions, you will need to uncheck "Bare Machine" and check "Accept Pseudo Instructions" (10 points) Declare an array of integers, something like: . data size : . word 8 array : . word 23 , -12 , 45 , -32 , 52 , -72 , 8 , 13 Write...
Which of the following files is a temporary file? a. transaction file b. master file c. reference file d. none of the above
Which of the following files is a temporary file? a. transaction fileb. master filec. reference filed. none of the above
Question 1 Download the files Book.java and BookInfo.txt from Content->Chapter 12 Quiz Files. Create a BookDriver.java...
Question 1 Download the files Book.java and BookInfo.txt from Content->Chapter 12 Quiz Files. Create a BookDriver.java class with a main method. In the main method, create an ArrayList of Book objects, read in the information from BookInfo.txt and create Book objects to populate the ArrayList. After all data from the file is read in and the Book objects added to the ArrayList- print the contents of the ArrayList. Paste your BookDriver.java text (CtrlC to copy, CtrlV to paste) into the...
In order to submit this assignment, you will use this webform to upload a document file...
In order to submit this assignment, you will use this webform to upload a document file (.doc) or (.docx) with your answers. If you need to make a graph you may use a drawing program or sketch by hand. Report your source in APA or MLA format. Be sure that your responses model clear economic reasoning and addresses each of the following: What are the advantages and disadvantages of a fixed exchange rate? What are the advantages and disadvantages of...
In order to submit this assignment, you will use this webform to upload a document file...
In order to submit this assignment, you will use this webform to upload a document file (.doc) or (.docx) with your answers. If you need to make a graph you may use a drawing program or sketch by hand. Report your source in APA or MLA format. Be sure that your responses model clear economic reasoning and addresses each of the following: What is the effect of expansionary fiscal policy on unemployment and inflation? Go online and find an article...
In order to submit this assignment, you will use this webform to upload a document file...
In order to submit this assignment, you will use this webform to upload a document file (.doc) or (.docx) with your answers. You may use a drawing program or sketch by hand. Be sure that your responses model clear economic reasoning and addresses each of the following: 1.) Suppose if the price of a good is $12, the quantity demanded is 50 units; when the price is $10, the quantity demanded is 100 units. Use the midpoint approach to compute...
You must download the file “Assn3_Qu#2_W19” to use the required data. It gives the number of...
You must download the file “Assn3_Qu#2_W19” to use the required data. It gives the number of city-bus users (Ridership) on a public transportation system of a large city in 3 given working days chosen at random in units of hundreds. It gives this data separately for the 4 busy bus routes and for 5 time slots. Here, TSlot1: from start of day to 9:30 am, TSlot2: 9:30 – 12:30, TSlot3: 12:30 – 15:30, TSlot4: 15:30 – 18:30 and Time-Slot5: 18:30...
You must download the file “Assn3_Qu#2_W19” to use the required data. It gives the number of...
You must download the file “Assn3_Qu#2_W19” to use the required data. It gives the number of city-bus users (Ridership) on a public transportation system of a large city in 3 given working days chosen at random in units of hundreds. It gives this data separately for the 4 busy bus routes and for 5 time slots. Here, TSlot1: from start of day to 9:30 am, TSlot2: 9:30 – 12:30, TSlot3: 12:30 – 15:30, TSlot4: 15:30 – 18:30 and Time-Slot5: 18:30...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT