-
Problem with Grading calculator in java program
Hello all.
I have this assignment for class and am running out of time to do this. It's due tomorrow at 11am and all of the tutors are gone since it's a Friday and I was in class all day. There are two parts to it and it's essentially a grading calculator.
The first part, the user has to input grades (integers) and once -1 is inputed, the program is supposed to stop. So I did most of it but now I'm just stuck on where to go and what's wrong. I need the program to print out what is on the bottom of the code. I'm sure you can get the gist of it once you read it.
*One of the requirements is to have one scanner in the first one and two scanners in the second. Also, the -1 that is put in can't count towards the grading calculations at the end.
Also, for the average, it has to rounded to two decimal places if needed, which is why I added in the decimal format, not sure if that's where it goes but it is needed.
In the second one, it's the same objective, but the grades are being read in from a .txt file that has grades inside of it. It has to work for any txt file given to it. Any suggestions on what to do for that code? It's pretty much the exact same...
Here's what I have. I know some of it is wrong, but this is all I could do. I also know it's missing things, I'm just not sure what they are. If anyone can help me with this that would be awesome. I'm in desperate need to get this done by tomorrow morning.
Code :
import java.util.Scanner;
public class Grades {
public static void main(String[] args) {
double sum = 0;
double average = 0;
int gradeCount = 0;
int highScore = 0;
int lowScore = 100;
int aCount = 0;
int bCount = 0;
int cCount = 0;
int dCount = 0;
int fCount = 0;
Scanner in = new Scanner(System.in);
System.out.println("Welcome to the Grade Calculator Program");
System.out.println("");
System.out.println("Please enter the student scores as integers between 0 and 100.");
System.out.println("Separate the scores with a space.");
System.out.println("Enter a negative number following the last student score.");
System.out.print("Enter the scores here: ");
while (jn.hasNext()) {
String line = in.nextLine();
gradeCount ++;
sum += grade;
average = sum/gradeCount;
DecimalFormat fmt = new DecimalFormat ("#.0#");
if (grade>=0)
if (grade < lowScore)
if (grade > highScore)
if (grade<=100)&&(grade>=90)
aCount ++;
if (grade>=80)&&(grade<=89)
bCount ++;
if (grade>=70)&&(grade<=79)
cCount ++;
if (grade>=60)&&(grade<=69)
dCount ++;
if (grade<=59)&&(grade>=0)
fCount ++;
if (grade<=0)
in.close;
}
System.out.println("Total number of grades : "+gradeCount);
System.out.println("High Score : "+highScore);
System.out.println("Low Score : "+lowScore);
System.out.println("Average Score : "+average);
System.out.println("Number of A's : "+aCount);
System.out.println("Number of B's : "+bCount);
System.out.println("Number of C's : "+cCount);
System.out.println("Number of D's : "+dCount);
System.out.println("Number of F's : "+fCount);
}
}
Thanks, I really hope someone can help me out with this. I really need it. The tutors are not here anymore at my school so I'm kinda desperate at this point.
And if I'm satisfied with the help someone gives me and get this to run, I'll send them some paypal money ;)
-Peter
-
Re: NEED HELP, Java Grading Assignment
Hello Peetah05 and welcome to the Java programming forums :)
I've edited this code in order to get it to compile. Im not sure if its producing your desired output though.
Code :
import java.util.Scanner;
import java.text.*;
public class Grades {
public static void main(String[] args) {
double sum = 0;
double average = 0;
int gradeCount = 0;
int highScore = 0;
int lowScore = 100;
int aCount = 0;
int bCount = 0;
int cCount = 0;
int dCount = 0;
int fCount = 0;
int grade = 0;
Scanner in = new Scanner(System.in);
System.out.println("Welcome to the Grade Calculator Program");
System.out.println("");
System.out.println("Please enter the student scores as integers between 0 and 100.");
System.out.println("Separate the scores with a space.");
System.out.println("Enter a negative number following the last student score.");
System.out.print("Enter the scores here: ");
while (in.hasNext()) {
String line = in.nextLine();
gradeCount ++;
sum += grade;
average = sum/gradeCount;
DecimalFormat fmt = new DecimalFormat("#.0#");
if (grade>=0)
if (grade < lowScore)
if (grade > highScore)
if ((grade<=100)&&(grade>=90))
aCount ++;
if ((grade>=80)&&(grade<=89))
bCount ++;
if ((grade>=70)&&(grade<=79))
cCount ++;
if ((grade>=60)&&(grade<=69))
dCount ++;
if ((grade<=59)&&(grade>=0))
fCount ++;
if (grade<=0)
in.close();
}
System.out.println("Total number of grades : "+gradeCount);
System.out.println("High Score : "+highScore);
System.out.println("Low Score : "+lowScore);
System.out.println("Average Score : "+average);
System.out.println("Number of A's : "+aCount);
System.out.println("Number of B's : "+bCount);
System.out.println("Number of C's : "+cCount);
System.out.println("Number of D's : "+dCount);
System.out.println("Number of F's : "+fCount);
}
}
Never good leaving things to the last minute!!
-
Re: NEED HELP, Java Grading Assignment
Thanks. It does run, but does not execute once you enter in the grades.
I get this error message:
Code :
Exception in thread "main" java.lang.IllegalStateException: Scanner closed
at java.util.Scanner.ensureOpen(Unknown Source)
at java.util.Scanner.hasNext(Unknown Source)
at Grades.main(Grades.java:31)
-
Re: NEED HELP, Java Grading Assignment
There is no way I can have this finished tonight. Sorry.. Hopefully someone else will post. Ill update first thing in the morning.
-
Re: NEED HELP, Java Grading Assignment
If you can do it by tomorrow morning at 11am that would be awesome. I'm gonna get up at 9am so that gives me 2 hours before it's due. I don't think it needs to be re-written, just a lot of editing and adding stuff. I'm only 2 months into java so this is kinda difficult for me.
Anyone else have any advice?
-
Re: NEED HELP, Java Grading Assignment
So the user enters grades into the console. You say as integers so its got to be numbers right? Rather than actual grades.
What scores do you have to get to have A, B, C, D, F grades?
I need an example input and a desired output. I need to understand this a bit better..
-
Re: NEED HELP, Java Grading Assignment
Yeah they are actual numbers, not letter grades. As listed in my code, an A is 100-90, B is 89-80, C is 79-70, D is 69-60, and F is 59-0. Then anything negative ends the loop (this is the number type at the end of your input to end it.
So a desired input would be: 86 94 48 58 91 -1.
Output:
Total number of grades: 5
High Score: 94
Low Score: 48
Average Score: 75.4
Number of A's: 2
Number of B's: 1
Number of C's: 0
Number of D's: 0
Number of F's: 2
-
Re: NEED HELP, Java Grading Assignment
Sorry I couldn't have it done for 11, I had to go out. DIdn't take many seconds though. Just took me about 3 to do it :P
Code :
import java.util.Scanner;
public class Grades {
public static void main(String[] args) {
double sum = 0;
double average = 0;
int gradeCount = 0;
int highScore = 0;
int lowScore = 100;
int aCount = 0;
int bCount = 0;
int cCount = 0;
int dCount = 0;
int fCount = 0;
int grade = 0;
Scanner in = new Scanner(System.in);
System.out.println("Welcome to the Grade Calculator Program");
System.out.println("");
System.out.println("Please enter the student scores as integers between 0 and 100.");
System.out.println("Separate the scores with a space.");
System.out.println("Enter a negative number following the last student score.");
System.out.print("Enter the scores here: ");
while (in.hasNext()) {
grade = Integer.parseInt(in.nextLine());
gradeCount ++;
sum += grade;
average = sum/gradeCount;
if (grade>=0){
if (grade < lowScore){
lowScore = grade;
}else if (grade > highScore){
highScore = grade;
}
if ((grade<=100)&&(grade>=90))
aCount ++;
if ((grade>=80)&&(grade<=89))
bCount ++;
if ((grade>=70)&&(grade<=79))
cCount ++;
if ((grade>=60)&&(grade<=69))
dCount ++;
if ((grade<=59)&&(grade>=0))
fCount ++;
}
else if (grade<0){
in.close();
break;
}
}
System.out.println("Total number of grades : "+gradeCount);
System.out.println("High Score : "+highScore);
System.out.println("Low Score : "+lowScore);
System.out.println("Average Score : "+average);
System.out.println("Number of A's : "+aCount);
System.out.println("Number of B's : "+bCount);
System.out.println("Number of C's : "+cCount);
System.out.println("Number of D's : "+dCount);
System.out.println("Number of F's : "+fCount);
}
}
PS I didn't add an error check for a "" input, so you may want to :P
Chris
-
Re: NEED HELP, Java Grading Assignment
Hey Chris,
Nice work although this doesnt work as its supposed to.
Your ment to enter scores in one line like: 23 34 23 100 98 12 -1
And the minus number causes the loop to stop and print out the grades.
-
Re: NEED HELP, Java Grading Assignment
Ah I see, ok re-do :P
Code :
import java.util.Scanner;
public class Grades {
public static void main(String[] args) {
double sum = 0;
double average = 0;
int gradeCount = 0;
int highScore = 0;
int lowScore = 100;
int aCount = 0;
int bCount = 0;
int cCount = 0;
int dCount = 0;
int fCount = 0;
int[] grades;
String[] tempGrades;
Scanner in = new Scanner(System.in);
System.out.println("Welcome to the Grade Calculator Program");
System.out.println("");
System.out.println("Please enter the student scores as integers between 0 and 100.");
System.out.println("Separate the scores with a space.");
System.out.println("Enter a negative number following the last student score.");
System.out.print("Enter the scores here: ");
tempGrades = in.nextLine().split(" ");
in.close();
grades = new int[tempGrades.length];
for(int j = 0; j < grades.length; j++) {
grades[j] = Integer.parseInt(tempGrades[j]);
if (grades[j]<0){
break;
}
gradeCount ++;
sum += grades[j];
average = sum/gradeCount;
if (grades[j]>=0){
if (grades[j] < lowScore){
lowScore = grades[j];
}else if (grades[j] > highScore){
highScore = grades[j];
}
if ((grades[j]<=100)&&(grades[j]>=90))
aCount ++;
if ((grades[j]>=80)&&(grades[j]<=89))
bCount ++;
if ((grades[j]>=70)&&(grades[j]<=79))
cCount ++;
if ((grades[j]>=60)&&(grades[j]<=69))
dCount ++;
if ((grades[j]<=59)&&(grades[j]>=0))
fCount ++;
}
}
System.out.println("Total number of grades : "+gradeCount);
System.out.println("High Score : "+highScore);
System.out.println("Low Score : "+lowScore);
System.out.println("Average Score : "+average);
System.out.println("Number of A's : "+aCount);
System.out.println("Number of B's : "+bCount);
System.out.println("Number of C's : "+cCount);
System.out.println("Number of D's : "+dCount);
System.out.println("Number of F's : "+fCount);
}
}
Chris
-
Re: NEED HELP, Java Grading Assignment
Nice one Chris! I'm sure this is what he was after although class is probably over now lol
Peetah05, next time you need help. Please give a bit more notice ;)
-
Re: NEED HELP, Java Grading Assignment
Hey Peetah05, just incase you still need it. I've modified Chris's code to read the scores from a text file which is what you need for part B.
Quote:
Write a program that reads the exam scores from a file. You are to write a separate program named GradesFromFile.java. This program will prompt the user for the name of the file, and then your program will read all the values from the file and perform the same calculations as you did in Part A.
PART B:
Code :
import java.util.Scanner;
import java.io.FileReader;
import java.io.FileWriter;
public class GradesFromFile {
public static void main(String[] args) throws Exception {
double sum = 0;
double average = 0;
int gradeCount = 0;
int highScore = 0;
int lowScore = 100;
int aCount = 0;
int bCount = 0;
int cCount = 0;
int dCount = 0;
int fCount = 0;
int[] grades;
String[] tempGrades;
Scanner in = new Scanner(System.in);
System.out.println("Welcome to the Grade Calculator Program");
System.out.println("");
System.out.print("Enter the text file eg scores.txt: ");
String txtFile = in.next();
FileReader fr = new FileReader(txtFile);
Scanner in2 = new Scanner(fr);
tempGrades = in2.nextLine().split(" ");
in2.close();
grades = new int[tempGrades.length];
for(int j = 0; j < grades.length; j++) {
grades[j] = Integer.parseInt(tempGrades[j]);
if (grades[j]<0){
break;
}
gradeCount ++;
sum += grades[j];
average = sum/gradeCount;
if (grades[j]>=0){
if (grades[j] < lowScore){
lowScore = grades[j];
}else if (grades[j] > highScore){
highScore = grades[j];
}
if ((grades[j]<=100)&&(grades[j]>=90))
aCount ++;
if ((grades[j]>=80)&&(grades[j]<=89))
bCount ++;
if ((grades[j]>=70)&&(grades[j]<=79))
cCount ++;
if ((grades[j]>=60)&&(grades[j]<=69))
dCount ++;
if ((grades[j]<=59)&&(grades[j]>=0))
fCount ++;
}
}
System.out.println("Total number of grades : "+gradeCount);
System.out.println("High Score : "+highScore);
System.out.println("Low Score : "+lowScore);
System.out.println("Average Score : "+average);
System.out.println("Number of A's : "+aCount);
System.out.println("Number of B's : "+bCount);
System.out.println("Number of C's : "+cCount);
System.out.println("Number of D's : "+dCount);
System.out.println("Number of F's : "+fCount);
}
}
You just need to create a .txt file, eg scores.txt containing the scores and save it in the programs root folder.
-
Re: NEED HELP, Java Grading Assignment
Thanks very much guys. I just tested the first one and it is what I needed. Only thing that I see that's needed is the decimal format command to round the average to two decimal places. I just tried adding it and am not sure where it is supposed to go. Kept getting errors.
-
Re: NEED HELP, Java Grading Assignment
Quote:
Originally Posted by
Peetah05
Thanks very much guys. I just tested the first one and it is what I needed. Only thing that I see that's needed is the decimal format command to round the average to two decimal places. I just tried adding it and am not sure where it is supposed to go. Kept getting errors.
I've added the DecimalFormat code so now the average is rounded to 2 decimal places.
You can see the changes in bold.
Code :
import java.util.Scanner;
[B]import java.text.*;[/B]
public class Grades {
public static void main(String[] args) {
double sum = 0;
double average = 0;
int gradeCount = 0;
int highScore = 0;
int lowScore = 100;
int aCount = 0;
int bCount = 0;
int cCount = 0;
int dCount = 0;
int fCount = 0;
int grade = 0;
[B] DecimalFormat df = new DecimalFormat("#.##");[/B]
Scanner in = new Scanner(System.in);
System.out.println("Welcome to the Grade Calculator Program");
System.out.println("");
System.out.println("Please enter the student scores as integers between 0 and 100.");
System.out.println("Separate the scores with a space.");
System.out.println("Enter a negative number following the last student score.");
System.out.print("Enter the scores here: ");
while (in.hasNext()) {
grade = Integer.parseInt(in.nextLine());
gradeCount ++;
sum += grade;
average = sum/gradeCount;
if (grade>=0){
if (grade < lowScore){
lowScore = grade;
}else if (grade > highScore){
highScore = grade;
}
if ((grade<=100)&&(grade>=90))
aCount ++;
if ((grade>=80)&&(grade<=89))
bCount ++;
if ((grade>=70)&&(grade<=79))
cCount ++;
if ((grade>=60)&&(grade<=69))
dCount ++;
if ((grade<=59)&&(grade>=0))
fCount ++;
}
else if (grade<0){
in.close();
break;
}
}
System.out.println("Total number of grades : "+gradeCount);
System.out.println("High Score : "+highScore);
System.out.println("Low Score : "+lowScore);
System.out.println("Average Score : "+[B]df.format(average)[/B]);
System.out.println("Number of A's : "+aCount);
System.out.println("Number of B's : "+bCount);
System.out.println("Number of C's : "+cCount);
System.out.println("Number of D's : "+dCount);
System.out.println("Number of F's : "+fCount);
}
}
-
Re: NEED HELP, Java Grading Assignment
In the second program, it compiles, but when I run it and enter in my txt file with some grades in it....I get this error message:
Exception in thread "main" java.io.FileNotFoundException: scores.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at GradesFromFile.main(GradesFromFile.java:30)
-
Re: NEED HELP, Java Grading Assignment
20 minutes and counting haha.
-
Re: NEED HELP, Java Grading Assignment
Quote:
Originally Posted by
Peetah05
In the second program, it compiles, but when I run it and enter in my txt file with some grades in it....I get this error message:
Exception in thread "main" java.io.FileNotFoundException: scores.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at GradesFromFile.main(GradesFromFile.java:30)
Yes that is because the scores.txt file cannot be found.
Make sure you save the file in the right directory.
My scores.txt file is located in the same directory as my projects bin and src directories.
Alternatively use this code below and make sure the file is saved on your C:\ drive. I've also adding the DecimalFormat code to the average value.
Code :
import java.util.Scanner;
import java.io.FileReader;
import java.io.FileWriter;
import java.text.*;
public class GradesFromFile {
public static void main(String[] args) throws Exception {
double sum = 0;
double average = 0;
int gradeCount = 0;
int highScore = 0;
int lowScore = 100;
int aCount = 0;
int bCount = 0;
int cCount = 0;
int dCount = 0;
int fCount = 0;
int[] grades;
DecimalFormat df = new DecimalFormat("#.##");
String[] tempGrades;
Scanner in = new Scanner(System.in);
System.out.println("Welcome to the Grade Calculator Program");
System.out.println("");
System.out.print("Enter the text file eg scores.txt: ");
String txtFile = in.next();
FileReader fr = new FileReader("C:\\" + txtFile);
Scanner in2 = new Scanner(fr);
tempGrades = in2.nextLine().split(" ");
in2.close();
grades = new int[tempGrades.length];
for(int j = 0; j < grades.length; j++) {
grades[j] = Integer.parseInt(tempGrades[j]);
if (grades[j]<0){
break;
}
gradeCount ++;
sum += grades[j];
average = sum/gradeCount;
if (grades[j]>=0){
if (grades[j] < lowScore){
lowScore = grades[j];
}else if (grades[j] > highScore){
highScore = grades[j];
}
if ((grades[j]<=100)&&(grades[j]>=90))
aCount ++;
if ((grades[j]>=80)&&(grades[j]<=89))
bCount ++;
if ((grades[j]>=70)&&(grades[j]<=79))
cCount ++;
if ((grades[j]>=60)&&(grades[j]<=69))
dCount ++;
if ((grades[j]<=59)&&(grades[j]>=0))
fCount ++;
}
}
System.out.println("Total number of grades : "+gradeCount);
System.out.println("High Score : "+highScore);
System.out.println("Low Score : "+lowScore);
System.out.println("Average Score : "+df.format(average));
System.out.println("Number of A's : "+aCount);
System.out.println("Number of B's : "+bCount);
System.out.println("Number of C's : "+cCount);
System.out.println("Number of D's : "+dCount);
System.out.println("Number of F's : "+fCount);
}
}
-
Re: NEED HELP, Java Grading Assignment
It's weird, the scores.txt file is right in the src folder with the java file. I'm not sure why it's not finding it.
-
Re: NEED HELP, Java Grading Assignment
Quote:
Originally Posted by
Peetah05
It's weird, the scores.txt file is right in the src folder with the java file. I'm not sure why it's not finding it.
Put it one directory up from that. Not in the src folder.. Put it in the directory that contains the src and bin folders.
But with that new code i've just posted, put it on your C:\ drive.
-
Re: NEED HELP, Java Grading Assignment
I got it to read the file but for some reason it's only reading the first grade in the text file. Mine was 98 and that's all the code found apparently...
-
Re: NEED HELP, Java Grading Assignment
The text file needs to be like this:
Not
-
Re: NEED HELP, Java Grading Assignment
Oh I see...Thanks again everyone for helping. Just submitted. Really appreciate it all.
-
Re: NEED HELP, Java Grading Assignment
Quote:
Originally Posted by
Peetah05
Oh I see...Thanks again everyone for helping. Just submitted. Really appreciate it all.
No problem.
You can pay us back by being an active member on the forums ;)
-
Re: NEED HELP, Java Grading Assignment
Glad we could help, ye more active members. BTW JavaPF, this site only appeared on google when i joined I think. SO glad I found it, neat little place this I like it. No big headed maniacs :P
And it's Java only, although a little C++ here and there would be ok :P
Chris