1

At the comment step 4 I am trying to add the current array element to sum, compare the current array element to max_test and if it is larger, save it in the variable max_test. and compare the current element to min_test, if it is smaller save it in min_test. HOWEVER i keep on getting the errors

Grades5.java:55: error: bad operand types for binary operator '>'
        if (grades[r] > grades[max_test]) 
                      ^
  first type:  int[]
  second type: int[]

Grades5.java:57: error: bad operand types for binary operator '<'
        if (grades[r] < grades[min_test])
                      ^
  first type:  int[]
  second type: int[]

Grades5.java:59: error: bad operand types for binary operator '+'
           sum += grades[r];
               ^
  first type:  int
  second type: int[]

3 errors

The code:

 import java.util.Scanner;

public class Grades5
{
  public static void main(String[] args)
  {
int[][] grades = {
                   { 87,  96, 100},
                   { 68,  75,  72},
                   { 99, 100,  95},
                   {100,  96,  70},
                   { 75,  60,  79},
                 };
int how_many_grades = grades.length * grades[0].length;

// -----------------
// Output the grades
// -----------------
System.out.print("           ");
for (int i = 0; i < grades[0].length; i++)
  System.out.print("Test " + (i + 1) + "  ");
System.out.println("Average");
for (int r = 0; r < grades.length; r++)
{
  int sum = 0;  // Sum of one student's tests

  // -------------------
  // Process one student
  // -------------------
  System.out.print("Student " + (r + 1) + "  ");
  for (int c = 0; c < grades[r].length; c++)
  {
    System.out.printf("%6d  ", grades[r]);                                                        // Step 1
      //sum += grades[c];                                                                     // Step 2
  }
  System.out.printf("%7.2f\n", (double)sum / grades[r].length);
}

// ----------------
// Output a summary
// ----------------
int max_test,  // Maximum test score
    min_test,  // Minimum test score
    sum = 0;   // Sum of all student tests

max_test = min_test = grades[0][0];    // Step 3
for (int r = 0; r < grades.length; r++)
{
  // -------------------
  // Process one student
  // -------------------
  for (int c = 0; c < grades[r].length; c++)
  {
                                     // Step 4
    if (grades[r] > grades[max_test]) 
      max_test = c; 
    if (grades[r] < grades[min_test])
       min_test = c;
       sum += grades[r];
  }
}
System.out.println("Highest test score: " + max_test);
System.out.println("Lowest  test score: " + min_test);
System.out.printf("Average test score: %.1f\n",
                  (double)sum / how_many_grades);
  }
}
3
  • Think about what grades[r] and grades[max_test] are, and what that means from your error message. It says you are trying to do int[] > int[], essentially compare two integer arrays, which is probably not what you intended to do. Commented Apr 5, 2016 at 20:59
  • It should be grades[r][c], not grades[r], because it is a bidimensional array. Commented Apr 5, 2016 at 20:59
  • You cannot simply change your question just because you got answers that fixed your original problem. Please accept an answer and ask another question if you think you're stuck - but be aware that the community does not really welcome problems which can be avoided by just using a debugger (at least if the problem is trivial). Commented Apr 5, 2016 at 21:38

4 Answers 4

1

Your problem is that grades[r] is not an integer. It is an array of integers. You'd have to address two indices

sum += grades[i][j];

in order to get rid of the compilation error.

It appears to be the same for the other errors. In general you can imagine it as follows:

grades[1] -> { 87,  96, 100}
grades[2] -> { 68,  75,  72}

and

grades[1][1] -> 87;
grades[1][2] -> 96;

etc ..

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

Comments

1

If the grades for one test are horizontal in your array, then you only need two loops, not three.

for (int test = 0; test < grades.length; test++) {
    System.out.print("Test " + (test + 1) + "  ");

    System.out.println("Average");
    int sum = 0; // Sum all grades on this test
    for (int student = 0; student < grades[test].length; student++)
    {
        System.out.print("Student " + (student + 1) + "  ");
        sum += grades[student];  
    }
    System.out.println();
    System.out.printf("%7.2f\n", (double)sum / grades[test].length);
}

Comments

1

You cannot use operator > to compare int[] types. Java doesn't support operator overloading. Those relational operators (>, <, >=, <=) are applied to numeric primitive data types only.

Do you mean something like grades[r][c] > grades[r][max_test] or grades[r][c] < grades[r][min_test]?

3 Comments

that produces another Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 87 at Grades5.main(Grades5.java:55)
@Brent, you're asking about ArrayIndexOutOfBoundsException 2 times for a short time, I suggest you reading about this exception and how to work with arrays.
@Brent, I don't have to write full working code for you
0

This works because grades needs 2 parts when comparing

    if (grades[r][c] > max_test) 
      max_test = grades[r][c]; 
    if (grades[r][c] < min_test)
      min_test = grades[r][c];
      sum += grades[r][c]; 

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.