Question
IO Exception Write a program that will read in a file of student academic credit data and create a list of students on academic warning.
IO Exception
Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written to a file. 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). The following shows part of a typical data file:
Smith 27 83.7
Jones 21 28.35
Walker 96 182.4
Doe 60 150
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) then write the student information to the output file if that student should be put on academic warning. A student will be on warning if he/she has a GPA less than 2.0. Do the following:
1. Create a class called Warning.java. In the main method, set up a Scanner object scan from the input file and a PrintWriter outFile to the output file. Recall that IOException is a checked exception. Use the throw clause way to handle it. Import the packages for input and output streams.
2. Write a loop structure to iteratively read from the input file. You can have the initial code as
while(scan.hasNextLine())
{
line = scan.nextLine();
System.out.println(line);
}
Run and test your program to make sure you can read in data smoothly. For any variable needed within the while loop, define it outside of the loop.
Your input file should be placed in the project folder. Use the following data:
students.dat
Smith 27 83.7
Jones 21 28.35
Walker 96 182.4
Doe 60 150
Wood 100 400
Street 33 57.4
Taylor 83 190
Davis 110 198
Smart 75 2 92.5
Bird 84 168
Summers 52 83.2
3. Now retrieve each token from the input line. The first should be saved as String type name, the second token should be saved as int type hours, and the third token should be a double type credits. You will need a second Scanner object to parse each input line into tokens. Again, declaration should be made outside of the loop structure.
4. Compute gpa using credits/hours. If gpa < 2.0, write the students name, hours, credits and gpa into the output file.
5. After the loop close the PrintWriter, and two Scanner objects.
6. Run your program. You should find the output file in the same project folder. Check the output data. You should have 6 students on the warning list. Note their gpa might have too many decimal places. Use the DecimalFormat class to format your output with only two decimal places. You can refer to section 3.6 on textbook for details. The general way to use it is
DecimalFormat df = new DecimalFormat("0.##");
pw.println(name+" "+df.format(gpa));
7. Think about the exceptions that could be thrown by this program:
A FileNotFoundException if the input file does not exist
A NumberFormatException if it cant parse an int or double when it tries to - this indicates an error in the input file format
An IOException if something else goes wrong with the input or output stream
Put a try block surrounding the loop structure, including all the decalaration of the input/output streams. Add a catch for each of these situations, and in each case give as specific a message as you can. The program will terminate if any of these exceptions is thrown, but at least you can supply the user with useful information.
8. Test the program. Given a wrong input file name, or include some mal data in the input file on purpose. See if the try-catch works as expected.
import java.io.*;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Warning {
public static void main(String[] args) throws IOException
{
try{
Scanner scan = new Scanner(new File("student.dat"));
PrintWriter pw = new PrintWriter("warninglist.txt");
DecimalFormat df = new DecimalFormat("0.##");
String line = "";
Scanner stringScan;
String name;
int hours;
double credits, gpa;
while(scan.hasNextLine())
{
line = scan.nextLine();
stringScan = new Scanner(line);
name = stringScan.next();
hours = stringScan.nextInt();
credits = stringScan.nextDouble();
gpa = credits/hours;
if(gpa<2.0)
pw.println(name+" "+df.format(gpa));
}
scan.close();
pw.close();
}
catch (FileNotFoundException exception)
{
System.out.println ("The file " + " was not found.");
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started