In: Computer Science
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 |
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--
