1 Attachment(s)
Letter grade isn't printing from the function to the output file...HELP!
Code :
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???
Re: Letter grade isn't printing from the function to the output file...HELP!
Quote:
Originally Posted by
112297
Formatting is always the last thing I do, so don't mind that...
I'm going to ask you to fix this now. The reason you format your code is to make it easier for others to read and understand. Since you're asking volunteers to do just that, to read and understand your code, a process that takes effort and time, I don't think it's asking too much for you to put in a little effort and time to make it easier for us to do this. Thanks in advance.
Re: Letter grade isn't printing from the function to the output file...HELP!
You mean the output or the actual code?
Re: Letter grade isn't printing from the function to the output file...HELP!
Quote:
Originally Posted by
112297
You mean the output or the actual code?
Oh, the code, especially the indenting and use of whitespace. Indentations should be uniform and consistent. Please note that the forum software works best if you use spaces, not tabs, and often the best number of spaces for an intent is between 3 and 4.
Re: Letter grade isn't printing from the function to the output file...HELP!
I fixed it...is that better?
Re: Letter grade isn't printing from the function to the output file...HELP!
I'll put the effort in for you this time since you're new, but this is what I mean by formatting:
Code java:
import java.io.*;
import java.util.*;
import java.lang.String;
public class Grade {
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;
}
}
For most of us code that is formatted well is much easier to read and to understand.
Now seeing this code, I can see what your problem is. You call the calculateGrade method here:
Code java:
while (gradesData.hasNext()) {
name = gradesData.next();
outfile.printf("%s ", name);
// ....
calculateGrade(stuAve);
outfile.printf("%.2f %n", stuAve);
outfile.print(grade);
talley = talley + stuAve;
}
But you never assign what is returned by the method into the grade variable, and so it remains holding a simple space, " ".
To fix this, simply assign the result returned by this method:
Code java:
while (gradesData.hasNext()) {
name = gradesData.next();
outfile.printf("%s ", name);
// ....
grade = calculateGrade(stuAve); // ****** here *****
outfile.printf("%.2f %n", stuAve);
outfile.print(grade);
talley = talley + stuAve;
}
Note the difference?
Re: Letter grade isn't printing from the function to the output file...HELP!
Really? I thought that's what I was doing inside of the function...So all I needed to do was assign grade the value of the function? Thank you so much Curmu! I'm going to try it now and let you know what happens...:-)
--- Update ---
It worked! Thank you! ...Now the fun part, formatting the output! lol
Re: Letter grade isn't printing from the function to the output file...HELP!
Glad you've got it working!
--- Update ---
I think that the key to understanding why it didn't work before, is that the variable, grade, located within the calculateGrade method is not the same grade variable in the main method. The first one is local to the calculateGrade method and thus exists only inside of this method.