0
public char[] calculateGrade(int [] scores, char [] grades){
    for (int r = 0; r < scores.length; r++){
        //System.out.println(scores[r] + " ");
        if (scores[r] > 90)
            grades[r] = 'A';
        else if (scores[r] > 80)
            grades[r] = 'B';
        else if (scores[r] > 70)
            grades[r] = 'C';
        else if (scores[r] > 60)
            grades[r] = 'D';
        else
            grades[r] = 'F';
    for (int i = 0; i < grades.length; i++)
        System.out.println(grades[i]);

    }
    return grades;

}

Above is a small part of my overall program that takes scores from a file and turns them into letter grades. I need to put these letter grades into that file. The letter grade needs to correspond with the score. There are 26 different scores one line at a time. I would put my entire code in here but it is really long. Please help guys!

2
  • 1
    The last for loop only prints all the grades multiple times. I am really confused... Commented Jul 12, 2011 at 23:53
  • By the way, I realize nobody else mentioned the problem with your code. Your loops are nested. So every time you read a score, you print all grades. Be careful with the brackets. Commented Aug 22, 2011 at 11:06

2 Answers 2

3

I'd recommend having each method do one thing and do it well. Don't calculate grades and print them out.

public void printGrades(PrintStream ps, char [] grades) {
    for (char grade : grades) {
       ps.println(grade);
    }
}

public char[] calculateGrade(int [] scores){

    char [] grades = new char[scores.length];

    for (int r = 0; r < scores.length; r++){
        if (scores[r] > 90)
            grades[r] = 'A';
        else if (scores[r] > 80)
            grades[r] = 'B';
        else if (scores[r] > 70)
            grades[r] = 'C';
        else if (scores[r] > 60)
            grades[r] = 'D';
        else
            grades[r] = 'F';
    }
    return grades;

}
Sign up to request clarification or add additional context in comments.

6 Comments

and I suppose, he only reads the scores from the file. So, "char [] grades" as a parameter of calculateGrade() is also a wrong approach.
I dont understand, you obviously are creating the grades array in the method and returning it to the class but how does the class understand it. Isnt it a local variable?
I took the "char [] grades" out of the parameter list after realizing that the cut & paste solution was not preferred.
So instead of printing the grades, how would I assign them to a file? Lets say there are 20 lines of someones name and their score, I need to put the letter grade after that score. How would I go about doing this? Sorry for the noobness...
You don't "assign" them to a file. You write to a file after opening it. Modify the method I gave you to include an array of names, scores and grades. Or, better yet, a List<Grade> or some other class that encapsulates name, score, and grade into a single unified thing. Java's an object-oriented language.
|
1

Change your method to just the following:

public void calculateGrade(int[] scores, char[] grades){
    for (int r = 0; r < scores.length; r++) {
        if (scores[r] > 90)
            grades[r] = 'A';
        else if (scores[r] > 80)
            grades[r] = 'B';
        else if (scores[r] > 70)
            grades[r] = 'C';
        else if (scores[r] > 60)
            grades[r] = 'D';
        else
            grades[r] = 'F';
    }
}

There's no need to return grades since arrays are passed by reference anyways. Then in your calling method (main?), you should write grades to the file you want.

To write to a file, consult: http://www.exampledepot.com/egs/java.io/WriteToFile.html

So something like:

try {
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("filename")));

    for( char grade : grades )
        out.println(grade);

    out.close();
} catch (IOException e) { }

2 Comments

Thanks man but the last part is a little too complicated lol. I am a junior CS student and I haven't learned about those keywords so far. So I need to keep it simple haha. But I understand the first part!
That's just how you write to a file. Don't worry about the try or the catch. Just think of out.println as similar to System.out.println except writing to a file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.