/** **************************************************************************** Warning.java Reads student data from a text file and writes data to another text file. **************************************************************************** */ import java.util.StringTokenizer; import java.io.*; import java.lang.*; import java.text.DecimalFormat; 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) { StringTokenizer tokenizer; String line, name, inputName = "students.dat"; String outputName = "warning.dat"; int creditHrs; // number of semester hours earned double qualityPts; // number of quality points earned double GPA; // grade point (quality point) average DecimalFormat fmt = new DecimalFormat("#.##"); try { //input file stream is inFile FileReader inStream = new FileReader(inputName); BufferedReader inFile = new BufferedReader(inStream); //output file stream is outFile FileWriter outStream = new FileWriter(outputName); BufferedWriter outputFile = new BufferedWriter(outStream); PrintWriter outFile = new PrintWriter(outputFile); // Print a header to the output file outFile.println(); outFile.println(" Students on Academic Warning"); outFile.println(); outFile.println("Name " + "Credit Hours " + "Quality Points " + "GPA"); outFile.println(); // Process the input file, one line at a time line = inFile.readLine(); while (line != null) { tokenizer = new StringTokenizer(line); /** 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. Read the next line of input */ name = tokenizer.nextToken(); try { creditHrs = Integer.parseInt(tokenizer.nextToken()); qualityPts = Double.parseDouble(tokenizer.nextToken()); GPA = qualityPts/creditHrs; if((GPA < 1.5 && creditHrs < 30) || (GPA < 1.75 && creditHrs < 60) || (GPA < 2.0 && creditHrs >= 60)) { outFile.print(name); //print name in output file int nameLength = name.length(); //Each if, else, and for space data out appropriately in output file if(creditHrs < 100) for(int i = 0; i < 10 - (nameLength - 5); i++) outFile.print(" "); else for(int i = 0; i < 10 - (nameLength - 4); i++) outFile.print(" "); outFile.print(creditHrs); //print creditHrs in output file if(qualityPts < 100) for(int i = 0; i < 12; i++) outFile.print(" "); else for(int i = 0; i < 11; i++) outFile.print(" "); outFile.print(qualityPts); //print qualityPts in output file if(qualityPts == 28.35) for(int i = 0; i < 9; i++) outFile.print(" "); else for(int i = 0; i < 10; i++) outFile.print(" "); outFile.println(fmt.format(GPA)); //print GPA in correct format to output file } } catch (NumberFormatException exception) { System.out.print("Error in input. Line ignored: "); System.out.println(line); } line = inFile.readLine(); } // Close both file streams inFile.close(); outFile.close(); } catch (FileNotFoundException exception) { System.out.println("The file " + inputName + " was not found."); } catch (IOException exception) { System.out.println(exception); } } }