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

Download the following zip file Bag and complete the program. You will need to complete files...
Download the following zip file Bag and complete the program. You will need to complete files MyBag and . MyBag uses the java API class ArrayList as the underline data structure. Using the class variables given, complete the methods of the MyBag class. In your BagHand class you should set appropriate default values for select class variables in the constructor in the add method, use the MyBag object to add a PlayingCard type to the bag and find and set...
submit the following files: Main.java Consider the following code: import java.util.Scanner; public class Main { public...
submit the following files: Main.java Consider the following code: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String input = ""; System.out.println("Enter the first number: "); input = in.nextLine(); int a = 0; int b = 0; a = Integer.parseInt(input); System.out.println("Enter the second number: "); input = in.nextLine(); b = Integer.parseInt(input); int result = 0; result = a/b; System.out.println("a/b : " + result); } Copy the code to your Main.java file...
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...
CAN SOMEONE PLEASE DO THIS QUESTION FOR ME. You are given three files Main.Java, Student.Java, LinkedList.java....
CAN SOMEONE PLEASE DO THIS QUESTION FOR ME. You are given three files Main.Java, Student.Java, LinkedList.java. Student is completed for you and is only used to test the LinkedList with an object (we will pass in Student for the generic parameter of the list). Your mission is to implement LinkedList.java, but I highly recommend you do this in a piecewise fashion as outlined in the //TODO s in Main.java (This assignment is easy if you do it piecewise and traumatic...
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...
For this assignment you are expected to submit the following two files: 1. ????ℎ??????ℎ??????. ℎ 2....
For this assignment you are expected to submit the following two files: 1. ????ℎ??????ℎ??????. ℎ 2. ????ℎ??????ℎ??????. ??? You are being asked to write a class for simple weather data storage. Each instance of the class will hold data for exactly one month (30 days). Each day’s weather will be classified as either rainy (‘R’), cloudy (‘C’), or sunny (‘S’). To achieve this, your class will contain a character array of length 30. The class will provide three functions to...
Use the Main.java file below to read in phrases (entire lines!) and print underscores "_" for...
Use the Main.java file below to read in phrases (entire lines!) and print underscores "_" for each character. Main.java already imports the Scanner class, creates your Scanner object, and reads in the first phrase from the keyboard. Your task is as followed: import java.util.Scanner; public class Main {    public static void main(String[] args) {        Scanner keyboard = new Scanner(System.in);        String phrase = keyboard.nextLine();           //Your code here!    } } Write a nested...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT