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 = 'A';
        else if (scores[r] > 80)
            grades = 'B';
        else if (scores[r] > 70)
            grades = 'C';
        else if (scores[r] > 60)
            grades = 'D';
        else
            grades = 'F';

        return grades;
    }
}

Above is one of my methods. It reads a part of a file (scores[]) and determines what letter grade they are. What I need to know how to do is store the letter grade in an array I have already crated called grades[].

Ardalan Shahgholi
  • 11,967
  • 21
  • 108
  • 144
Josh
  • 163
  • 2
  • 7
  • 23
  • 1
    Why don't you consider using a switch statement as well? http://download.oracle.com/javase/tutorial/java/nutsandbolts/switch.html – Joeblackdev Jul 12 '11 at 22:58
  • Hi Josh, Also note your return is within the for loop which is wrong for what you want. I'd use the grades[r] = 'A' etc as suggested below but drop the return outside of the for loop – Jim Jul 12 '11 at 22:59
  • Thanks so much guys. Joel, my actual program is much bigger than this with a switch statement in it that calls this. – Josh Jul 12 '11 at 23:03

7 Answers7

2

grades[r] = 'A'; (and similar for others)

This assumes grades is of same length as scores

Ross Larson
  • 2,357
  • 2
  • 27
  • 38
  • Do I return grades[]? or grades? or what? And thank you by the way, I could have sworn I tried sub r. – Josh Jul 12 '11 at 22:55
  • just return grades it is an object (a char array in fact). Also change return type to char[] (you are returning the whole array of grades, correct?), or as Tamas does, return nothing, and just leave the results in grades and access grades (might as well since you are passing it in) – Ross Larson Jul 12 '11 at 22:57
  • Thank you. In the method signature what should be my return value? grade[]? – Josh Jul 12 '11 at 22:58
  • either void (see Tamas's answer) and just access grades from where you pass it to calculateGrade, or char[] if you wish to return the char array you call grades – Ross Larson Jul 12 '11 at 23:00
  • no problem josh, also i hope you saw the comment about moving the return statement out of loop (unless you remove it completely) – Ross Larson Jul 12 '11 at 23:05
  • Yea I found that on my own then I saw the comment. OK one more thing, after each grade letter is stored in the array for each score, how do I print them? Are they even stored seperatley? Sorry for all my noobness – Josh Jul 12 '11 at 23:08
  • http://stackoverflow.com/questions/409784/simplest-way-to-print-an-array-in-java may help? – Ross Larson Jul 12 '11 at 23:10
  • I can get it to print in char but I cant get it to print each grade letter on a seperate line. – Josh Jul 12 '11 at 23:17
  • for (char c : grades) { System.out.println(c); } (obviously format that prettier) That is a for each loop (to be honest in future this should be a separate question, but this time i'll just answer here) – Ross Larson Jul 12 '11 at 23:58
  • Thanks dude! Please one more man, I have a file already made that has 20 lines with names and their score next to them. I've already calculated the letter grade for each score I just need to print them corresponding with their score(one per line). Please help! – Josh Jul 13 '11 at 00:17
  • Sorry, there is a bit to much to that to answer in comments. Post up another question with some more specifics (you want to print to console or file?, and do you have to read in from a file?). If its still unanswered by the time I get to it (probably not until tomorrow aft) I will give it a shot then, but ti will probably be answered quickly bby someone else – Ross Larson Jul 13 '11 at 02:04
0

This looks suspiciously like a homework assignment, so look at how you access the scores array, and how you access the grades array. Do you see the difference?

Jason Day
  • 8,809
  • 1
  • 41
  • 46
0

Try this:

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';
    }
}
Tamás
  • 298
  • 3
  • 14
0
grades = 'A';

A is of type char while the identifier grades is of type char[]. What you have to do is to accumulate the grades in the array using [] operator. Also you should return grades once you are done with the for loop and not at the first iteration. Make sure that the length of array grades is equivalent to scores.

Mahesh
  • 34,573
  • 20
  • 89
  • 115
0
public char calculateGrade(int[] scores)
{
    char grade = 'F';
    for(int score : scores)
    {
        if(score >= 90) grade = 'A';
        else if(score >= 80) grade = 'B';
        else if(score >= 70) grade = 'C';
        else if(score >= 60) grade = 'D';
        else grade = 'F';
    }
    return grade;
}
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • Yes, but ... "What I need to know how to do is store the letter grade in an array I have already crated called grades[]." –  Jul 12 '11 at 23:13
0

Avoid side-effects in functions, where possible, consider this as an alternative approach:

// Given a score, return the appropriate Grade for it.
public char calculateGrade(int score){
   // See function contract.
}

// Later...
int scores[] = {10,70,50,90};
int grades[] = new char[scores.length]; // but really, use an ArrayList or simialr...
for (var i = 0; i < scores.length; i++) {
   // We used this form of "for" to keep an index
   grades[i] = calculateGrade(scores[i]);
}

// And perhaps there might be a function like this as well:

// Given a set of scores, get the associated grades.
public char[] calculateGrades(int[] scores) {
   // See above.
   // Note how this avoids mutating an input parameter.
   // Keeping functions "more refined" also generally helps.
}

Happy coding.

0

All of what I'm seeing doesn't consider the fact that it would seem as though you are attempting to pass by reference, which cannot be done in java. You either need to change the return type of your method (currently it is expecting a single character), or have the method just access a global variable, rather than passing it a variable. It does look like homework so I'm not going to go any further.

Dwight
  • 360
  • 1
  • 3
  • 15