I can't figure out what im doing wrong with my File I/O program lol
807580Feb 10 2010 — edited Feb 10 2010I'm new to the java language and was wondering if anybody could help me with the program i an writing. I don't quite understand file I/O but I've given it my best and I'm stuck. I'm supposed to 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 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 1.5 for students with fewer than 30 semester hours credit, 1.75 for students with fewer than 60 semester hours credit, and 2.0 for all other students. The instructions are :
1. Set up a Scanner object scan from the input file and a PrintWriter outFile to the output file inside the try
clause (see the comments in the program). Note that youll have to create the PrintWriter from a FileWriter,
but you can still do it in a single statement.
2. Inside the while loop add code to read the input fileget the name, the number of credit hours, and the
number of quality points. Compute the GPA, determine if the student is on academic warning, and if so
write the name, credit hours, and GPA (separated by spaces) to the output file.
3. After the loop close the PrintWriter and Scanner objects in a finally block.
4. Think about the exceptions that could be thrown by this program:
A FileNotFoundException if the input file does not exist
A InputMismatchException if it cant read 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
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.
5. Test the program. Test data is in the file students.txt. Be sure to test each of the exceptions as well.
My source code is:
// ****************************************************************************
// Warning.java
//
// Reads student data from a text file and writes data to another text file.
// ****************************************************************************
import java.util.*;
import java.io.*;
public class Warning
{
// --------------------------------------------------------------------
// Reads student data (name, semester hours, quality points) from a
// text file, computes the GPA, then writes data to another file
// if the student is placed on academic warning.
// --------------------------------------------------------------------
public static void main (String[] args)
{
int creditHrs; // number of semester hours earned
double qualityPts; // number of quality points earned
double gpa; // grade point (quality point) average
Scanner scan=null;
PrintWriter outFile=null;
String name, inputName = "students.txt";
String outputName = "warning.txt";
try
{
// Set up Scanner to input file
scan=new Scanner(new FileInputStream(inputName));
// Set up the output file stream
outFile = new PrintWriter(new FileWriter(outputName));
// Print a header to the output file
outFile.println ();
outFile.println ("Students on Academic Warning");
outFile.println ();
// Process the input file, one token at a time
while (scan.hasNext())
{
// Get the credit hours and quality points and
// determine if the student is on warning. If so,
// write the student data to the output file.
name=scan.next();
creditHrs=scan.nextInt();
qualityPts=scan.nextDouble();
gpa=qualityPts/creditHrs;
if ((gpa < 1.5 && creditHrs < 30) || (gpa < 1.75 && creditHrs < 60) || (gpa < 2.0 && creditHrs >= 60))
{
outFile.print(name + " ");
outFile.print(creditHrs + " ");
outFile.print(qualityPts + " ");
outFile.print(gpa);
}
}
}
//Add a catch for each of the specified exceptions, and in each case
//give as specific a message as you can
catch (FileNotFoundException e)
{
System.out.println("The file " + inputName + " was not found.");
}
catch (IOException e)
{
System.out.println("The I/O operation failed and " + outputName + " could not be created.");
}
catch (InputMismatchException e)
{
System.out.println("The input information was not of the right type.");
}
//Close both files in a finally block
finally
{
scan.close();
outFile.close();
}
}
}
The txt file is:
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 292.5
Bird 84 168
Summers 52 83.2
The program will run and then terminate without creating warning.txt. How can I get it to create the warning .txt file? Any help that you could give would be greatly appreciated.