1 Replies - 1868 Views - Last Post: 16 October 2010 - 02:36 PM Rate Topic: -----

#1 TheBewitchedOne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-October 10

Having 2D array trouble

Posted 16 October 2010 - 02:30 PM

Hi,
I am working on a Java 2D Array to input 3 grades for 10 students.
I can't seem to get it right.
Any help will be greatly appreciated.

import java.util.Scanner;
import java.io.*;



public class Assign06_Cheshire{
  public static void main(String[] args){


    Scanner input=new Scanner(System.in);

    int midTerm1=0;
    int midTerm2=0;
    int finalExam=0;

    int[][]grades= new int[10][3];


    System.out.println("Enter the 10 Midterm 1 grades: ");

        for (int i = 0; i < grades.length; i++){
           for (int j = 0; j < grades.length ; j++){
            System.out.println(" MidTerm Grade "+(i+1)+": ");
            grades[i][j]=input.nextInt();


        }


      System.out.print("Enter the 10 Midterm 2 grades: ");
        for (int i = 0; i < grades.length; i++){
           for (int j = 0; j < grades.length; j++)
            System.out.print("Midterm Grade "+(i+1)+": ");
            grades[i][j]=input.nextInt();

        }
        System.out.print("Enter the 10 Final Exam grades: ");
        for (int i = 0; i < grades.length; i++){
           for (int j = 0; j < grades.length; j++)
            System.out.print("Final Exam Grade "+(i+1)+": ");
            grades[i][j]=input.nextInt();
     }
        //add together the midterm 1 midterm 2 and final exam
        for (int i=0;i<10;i++)
            grade[i]=midterm1[i]+midterm2[i]+finalExam[i];


        //calculate min and max grade
        int minGrade=grade[0];
        int maxGrade=grade[0];
        for (int i=1;i<10;i++)
        {
            if (minGrade>grade[i])
                minGrade=grade[i];
            if (maxGrade<grade[i])
                maxGrade=grade[i];
        }

        //display min and max grade
        System.out.println("The minimum grade is "+minGrade);
        System.out.println("The maximum grade is "+maxGrade);
    }
}

           }


Errors:
i is already defined in main(java.lang.String[])
cannot find symbol variable grade
cannot find symbol variable j

Is This A Good Question/Topic? 0
  • +

Replies To: Having 2D array trouble

#2 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Having 2D array trouble

Posted 16 October 2010 - 02:36 PM

After the first time you do "int i = 0" in the first for loop, you can no longer do that again. It is already declared.

You can do it like this:
for(i = 0;.....)



Hope this helps! If any more problems occur let me know!

This post has been edited by eZACKe: 16 October 2010 - 02:36 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1