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
Jones
Walker
Doe
Wood
Street
Taylor
Davis
Smart
Bird
Summers The 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 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. points
Display the name if the gpa is less than points
Test to ensure the output is accurate. points
Add code to catch the following exceptions:
A FileNotFoundException if the input file does not exist.
How will you test for this exception? 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? points Warning. java
import java.util.Scanner;
import
java.io;
public class Warning
double qualityPts; number of quality points earned String name; Scanner inFile new Scannernew Filec:studentsdat"; Process the input file, one token at a time determine if the student is on warning. If so name inFile.next; qualityPts Double.parseDoubleinFilenext; 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 by subtracting A or decimal 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 nonletter will generate an index that is not between and It might be desirable to allow nonletter
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 nonletter 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. points
b Add a catch that catches the exception, but don't do anything with it points
Now modify the body of the catch so that it prints a useful message eg "Not a letter" followed by the
exception. points CountLetters.java
import java.util.Scanner;
public class CountLetters
Scanner scan new ScannerSystemin; System.out.print
Enter a single word letters only: ; convert to all upper case count frequency of each letter in string countsword charAtiA;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
