Question: Exceptions Aren't Always Errors Reading from Text File Review the program Warning.java that reads in a file of student academic credit data. Each line of

Exceptions Aren't Always Errors Reading from Text File
Review the program Warning.java that reads in a file of student academic credit data. Each line of the input file
will contain the student name (a single String with no spaces), the number of semester hours earned (an integer),
the total quality points earned (a double).
Here is the students.dat data file:Smith 2783.7
Jones 2128.35
Walker 96182.4
Doe 60150
Wood 100400
Street 3357.4
Taylor 83190
Davis 110198
Smart 75292.5
Bird 84168
Summers 5283.2The program should compute the GPA (grade point or quality point average) for each student (the total quality
points divided by the number of semester hours), and display the student name if the gpa is less than 2.0. The
file Warning.java contains a skeleton of the program. After adding the comment section, do the following:
Add the code to calculate the gpa. (3 points)
Display the name if the gpa is less than 2.0.(3 points)
Test to ensure the output is accurate. (3 points)
Add code to catch the following exceptions:
A FileNotFoundException if the input file does not exist.
How will you test for this exception? (3 points)
A NumberFormatException if it can't parse an int or double when it tries to - this indicates an error
in the input file format.
How will you test for this error? (3 points)// Warning. java
//****************************************************************************
import java.util.Scanner;
import
java.io.*;
public class Warning
1{ double qualityPts; // number of quality points earned String name; Scanner inFile = new Scanner(new File("c:\\students.dat")); // Process the input file, one token at a time {// determine if the student is on warning. If so, name = inFile.next(); qualityPts = Double.parseDouble(inFile.next()); // and statement to determine if the student name is listed. inFile.close();)
}
File CountLetters.java contains a program that reads a word from the user and prints the number of occurrences
of each letter in the word. In reviewing the code, note that the word is converted to all upper case first, then
each letter is translated to a number in the range 0..25(by subtracting 'A' or decimal 65) for use as an index. No
test is done to ensure that the characters are in fact letters.
Add the proper comment section, run CountLetters and enter a phrase, that is, more than one word with
spaces or other punctuation in between. It should throw an ArrayIndexOutOfBoundsException, because
a non-letter will generate an index that is not between 0 and 25. It might be desirable to allow non-letter
characters, but not count them. Of course, you could explicitly test the value of the character to see if it
is between 'A' and 'Z'. However, an alternative is to go ahead and use the translated character as an
index, and catch an ArrayIndexOutOfBoundsException if it occurs. Since you want don't want to do
anything when a non-letter occurs, the handler will be empty. Modify this method to do this as follows:
a. Put the body of the first for loop in a try. (5 points)
b. Add a catch that catches the exception, but don't do anything with it.(5 points)
Now modify the body of the catch so that it prints a useful message (e.g., "Not a letter") followed by the
exception. (5 points)// CountLetters.java
//****************************************************************************
import java.util.Scanner;
public class CountLetters
1{ Scanner scan = new Scanner(System.in); System.out.print("
Enter a single word (letters only): "); //convert to all upper case //count frequency of each letter in string counts[word. charAt(i)-'A']++;
Exceptions Aren't Always Errors Reading from Text

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!