- Mar 13, 2005
- 145
- 0
- 0
Ok, this assignment is to read a list of grades from a .txt file and find the averages, and assign a grade, and send the output to another .txt file. Here's my source so far, and the error messages i'm getting. Any help is GREATLY appreciated.
------------------------------------
package Lab2;
/*This program reads a student's name and test scores.
*It then computes average test score for each student and assigns
*appropriate grade.
*/
import java.io.*;
import javax.swing.*;
import java.util.*;
public class Lab2 {//start class Lab2
public static float calculateAverage(StringTokenizer token, PrintWriter outFile)
{//this method returns values for avg of scores.
float total = 0;
while (token.hasMoreTokens()) {
String m = token.nextToken();
int i = Integer.parseInt(m);
total = (total + i);
}//end while
float avg = (total / 5);
return avg;
}//end method calculateAverage
public static char calculateGrade(double avg)
{//this method determines and returns each student's grade.
char grade;
if (avg < 60)
grade = 'F';
else if (avg >= 60 && avg < 70)
grade = 'D';
else if (avg >= 70 && avg < 80)
grade = 'C';
else if (avg >= 80 && avg < 90)
grade = 'B';
else if (avg >= 90 && avg <= 100)
grade = 'A';
return grade;
}//end method calculateGrade
public static void main(String[] args)
{//main method- opens studentfile.txt, sends output to studentout.txt,
BufferedReader inFile = new BufferedReader (new FileReader ("D:\\studentfile.txt"));
PrintWriter outFile = new PrintWriter (new FileWriter ("D:\\studentout.txt"));
outFile.println ("Student Test1 Test2 Test3 Test4 Test5 Average Grade");
String s;
while ((s = inFile.readLine()) != null) {
outFile.println (s);
StringTokenizer token = new StringTokenizer (s);
float avg = calculateAverage (token, outFile);
char grade = calculateGrade (avg);
outFile.println (avg + " " + grade);
}//end while
}//end main
}//end class Lab2
---------------------------
error: unreported exception: java.io.FileNotFoundException; must be caught or declared to be thrown.
(this is on the BufferedReader line)
error: unreported exception: java.io.IOException; must be caught or declared to be thrown.
(this is on the PrintWriter line)
I realize I need a few more lines of code in the main method, but I am stuck as to why the compiler cannot find the input and output files....
------------------------------------
package Lab2;
/*This program reads a student's name and test scores.
*It then computes average test score for each student and assigns
*appropriate grade.
*/
import java.io.*;
import javax.swing.*;
import java.util.*;
public class Lab2 {//start class Lab2
public static float calculateAverage(StringTokenizer token, PrintWriter outFile)
{//this method returns values for avg of scores.
float total = 0;
while (token.hasMoreTokens()) {
String m = token.nextToken();
int i = Integer.parseInt(m);
total = (total + i);
}//end while
float avg = (total / 5);
return avg;
}//end method calculateAverage
public static char calculateGrade(double avg)
{//this method determines and returns each student's grade.
char grade;
if (avg < 60)
grade = 'F';
else if (avg >= 60 && avg < 70)
grade = 'D';
else if (avg >= 70 && avg < 80)
grade = 'C';
else if (avg >= 80 && avg < 90)
grade = 'B';
else if (avg >= 90 && avg <= 100)
grade = 'A';
return grade;
}//end method calculateGrade
public static void main(String[] args)
{//main method- opens studentfile.txt, sends output to studentout.txt,
BufferedReader inFile = new BufferedReader (new FileReader ("D:\\studentfile.txt"));
PrintWriter outFile = new PrintWriter (new FileWriter ("D:\\studentout.txt"));
outFile.println ("Student Test1 Test2 Test3 Test4 Test5 Average Grade");
String s;
while ((s = inFile.readLine()) != null) {
outFile.println (s);
StringTokenizer token = new StringTokenizer (s);
float avg = calculateAverage (token, outFile);
char grade = calculateGrade (avg);
outFile.println (avg + " " + grade);
}//end while
}//end main
}//end class Lab2
---------------------------
error: unreported exception: java.io.FileNotFoundException; must be caught or declared to be thrown.
(this is on the BufferedReader line)
error: unreported exception: java.io.IOException; must be caught or declared to be thrown.
(this is on the PrintWriter line)
I realize I need a few more lines of code in the main method, but I am stuck as to why the compiler cannot find the input and output files....
