1

Can anyone tell me what I am doing wrong in this code?

public class grades {
    public static void main(String[] args) {
        int[] testMarks = Marks.getMarks();
        System.out.print(grading(testMarks));

    }
    public static char[] grading(int[] testMarks) {

        char grade = '0';
        char[] grades = new char[grade];
        int value = 0;
        int n = 0;
            while (n < testMarks.length) {
                value = testMarks[n];
                if (value >= 90) {
                grade = 'A';
                }
                else if (value < 90 && value >= 75) {
                grade = 'B';
                }
                else if (value < 75 && value >= 60) {
                grade = 'C';
                }
                else if (value < 60 && value >= 50) {
                grade = 'D';
                }
                else if (value < 50 && value >= 45) {
                grade = 'E';
                }
                else {
                grade = 'F';
                }
                n =+ 1;
        }
        return grades;
    }
}

I am trying to return the values of the testMarks array as char based on the boundaries listed. I don't get any errors, but it doesn't display anything.

Cheers, Em

1
  • That should be n += 1. And where is Marks class defined? Also, the class name should start with capital letter I guess! Commented Aug 21, 2016 at 8:34

4 Answers 4

3
  • n =+ 1 would be n += 1.
  • char[] grades = new char[grade] would be char[] grades = new char[testMarks.length]
  • before increament of n, write grades[n] = grade
Sign up to request clarification or add additional context in comments.

Comments

1

You forgot to populate the grades array :) you need to:

grades[n] = grade;

before incrementing n; otherwise the grades array would be empty. Other than that, n++ would work just fine:

public static char[] grading(int[] testMarks) {

    char grade = '0';
    char[] grades = new char[testMarks.length];
    int value = 0;
    int n = 0;
        while (n < testMarks.length) {
            value = testMarks[n];
            if (value >= 90) {
            grade = 'A';
            }
            else if (value < 90 && value >= 75) {
            grade = 'B';
            }
            else if (value < 75 && value >= 60) {
            grade = 'C';
            }
            else if (value < 60 && value >= 50) {
            grade = 'D';
            }
            else if (value < 50 && value >= 45) {
            grade = 'E';
            }
            else {
            grade = 'F';
            }
            grades[n] = grade;
            n++;
    }
    return grades;
}

Comments

0

Maybe you should write n+=1 in your code

Comments

0

grades array is always empty. You need to store the grade values in that array before returning. Something like this should work:

public static char[] grading(int[] testMarks) {

    char grade = '0';
    char[] grades = new char[testMarks.length]; // initialise with an empty char array with the same length as testMarks
    int value = 0;
    int n = 0;
    while (n < testMarks.length) {
        value = testMarks[n];
        if (value >= 90) {
            grade = 'A';
        }
        else if (value < 90 && value >= 75) {
            grade = 'B';
        }
        else if (value < 75 && value >= 60) {
            grade = 'C';
        }
        else if (value < 60 && value >= 50) {
            grade = 'D';
        }
        else if (value < 50 && value >= 45) {
            grade = 'E';
        }
        else {
            grade = 'F';
        }
        grades[n] = grade; // store the grade in the grades array
        n += 1;
    }
    return grades;
}

Comments

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.