import java.io.*; import java.util.*; import java.lang.String; import java.lang.*; public class grades { public static double stuAve = 0.0; public static double classAve = 0.0; //public static String grade = " "; public static void main (String[] args) throws FileNotFoundException { String name; int s1, s2, s3, s4, s5; double talley = 0.0; //double stuAve, classAve; String grade = " "; Scanner gradesData = new Scanner(new FileReader("F:\\gradesData.txt")); PrintWriter outfile = new PrintWriter("F:\\grades.txt"); // classAve = 0.0; outfile.println(" Student Test1 Test2 Test3 Test4 Test5 Average Grade"); while(gradesData.hasNext()) { name = gradesData.next(); outfile.printf("%s ", name); s1 = gradesData.nextInt(); outfile.printf("%-9d " , s1); s2 = gradesData.nextInt(); outfile.printf("%-7d " , s2); s3 = gradesData.nextInt(); outfile.printf("%-7d " , s3); s4 = gradesData.nextInt(); outfile.printf("%-7d " , s4); s5 = gradesData.nextInt(); outfile.printf("%-7d " , s5); calculateAverage(s1, s2, s3, s4, s5); calculateGrade(stuAve); outfile.printf("%.2f %n", stuAve); outfile.print(grade); talley= talley + stuAve; } classAve = talley /10.0; outfile.printf("Class Average = %.2f %n", classAve); gradesData.close(); outfile.close(); public static void calculateAverage(int s1,int s2, int s3,int s4,int s5) { stuAve = ((double) s1 + (double)s2 + (double)s3 + (double)s4 + (double)s5)/5.0; } public static String calculateGrade(double stuAve) { String grade = " "; if(stuAve >= 93) grade = "A"; else if(stuAve >= 85 && stuAve <=92) grade = "B"; else if(stuAve >= 76 && stuAve <=84) grade = "C"; else if(stuAve >= 70 && stuAve <=75) grade = "D"; else grade = "F"; // outfile.print(grade); return grade; } }
Here is the output to the file:
Student Test1 Test2 Test3 Test4 Test5 Average Grade
Johnson 85 83 77 91 76 82.40
Aniston 80 90 95 93 48 81.20
Cooper 78 81 11 90 73 66.60
Gupta 92 83 30 69 87 72.20
Blair 23 45 96 38 59 52.20
Clark 60 85 45 39 67 59.20
Kennedy 77 31 52 74 83 63.40
Bronson 93 94 89 77 97 90.00
Sunny 79 85 28 93 82 73.40
Smith 85 72 49 75 63 68.80
Class Average = 70.94
Formatting is always the last thing I do, so don't mind that...I have used both char and string to try and return the letter grade value from the calculateGrade function..I have tried making 'grade' a local, global, and regular variable of both the string and char types...still nothing...What gives???