Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 24 of 24

Thread: Problem with Grading calculator in java program

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

    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
    Last edited by Deep_4; November 7th, 2012 at 12:17 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default 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.

    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!!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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:

    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)

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default 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.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  5. #5
    Junior Member
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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?

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default 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..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  7. #7
    Junior Member
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  8. #8
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default 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

    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

    Chris

  9. #9
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default 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.

    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  10. #10
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: NEED HELP, Java Grading Assignment

    Ah I see, ok re-do

    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

  11. #11
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default 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
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  12. #12
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default 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.

    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:

    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.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  13. #13
    Junior Member
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  14. #14
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Talking Re: NEED HELP, Java Grading Assignment

    Quote Originally Posted by Peetah05 View Post
    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.

    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);
     
        }
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  15. #15
    Junior Member
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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)

  16. #16
    Junior Member
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: NEED HELP, Java Grading Assignment

    20 minutes and counting haha.

  17. #17
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Talking Re: NEED HELP, Java Grading Assignment

    Quote Originally Posted by Peetah05 View Post
    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.

    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);
     
        }
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  18. #18
    Junior Member
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  19. #19
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: NEED HELP, Java Grading Assignment

    Quote Originally Posted by Peetah05 View Post
    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.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  20. #20
    Junior Member
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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...

  21. #21
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: NEED HELP, Java Grading Assignment

    The text file needs to be like this:

    22 34 100 90 98 92 55 24

    Not

    22 
    34
    100 
    90 
    98 
    92 
    55 
    24
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  22. #22
    Junior Member
    Join Date
    Mar 2009
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: NEED HELP, Java Grading Assignment

    Oh I see...Thanks again everyone for helping. Just submitted. Really appreciate it all.

  23. #23
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: NEED HELP, Java Grading Assignment

    Quote Originally Posted by Peetah05 View Post
    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
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  24. #24
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default 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

    And it's Java only, although a little C++ here and there would be ok

    Chris

Similar Threads

  1. Replies: 1
    Last Post: May 8th, 2009, 08:55 AM
  2. New assignments for JAVA beginners
    By Peetah05 in forum File I/O & Other I/O Streams
    Replies: 24
    Last Post: April 24th, 2009, 11:33 AM
  3. [SOLVED] "GridLayout" problem in Java program
    By antitru5t in forum AWT / Java Swing
    Replies: 3
    Last Post: April 16th, 2009, 10:26 AM
  4. Java program for to implement supermarket menu
    By 5723 in forum AWT / Java Swing
    Replies: 1
    Last Post: April 14th, 2009, 03:14 AM