# help w/ storing/scanning numbers in two dimensional arrays

• November 30th, 2010, 07:38 PM
robertsbd
help w/ storing/scanning numbers in two dimensional arrays
have a assignment that we have to accomplish the same work as in a previous assignment except use two dimensional arrays... this is my previous assignment

A Computer Technology Instructor has a small class of 10 students. The instructor evaluates the performance of students in the class by administering 2 midterm tests and a Final Exam.

Write a program that prompts the instructor to enter the 10 grades of Midterm 1 and store these numbers in an array. Next prompt for the 10 grades of Midterm2 and store these numbers in a different array. Next prompt for the 10 grades of the Final Exam and store these in a different array. Next add Midterm1 to Midterm2 to Final and store the total of grades in a different array. Next, scan the array that has the totals and identify the minimum grade and maximum grade. Inform the instructor of the minimum grade and maximum grade.

Note : do not assume that the grades are in the range 0 to 100. Your program should function properly whether the grades are in the range 0 to 100 or any other range.

*****and this is my next assignments instructions

Use one two-dimensional array to accomplish the same work you did in the last Assignment. Think of the students as being the columns of the two-dimensional array. Think of the scores of Midterm 1 as occupying the first row, scores of Midterm 2 occupying the next row, scores of the Final Exam occupying the next row. The total of the 3 exams occupying the next row. Inform the instructor of the minimum total grade and the maximum total grade.

***and here is my code, got errors all over the place and not sure where to begin or iif im even in the ballpark

Code java:

```  import java.util.Scanner; import java.io.*;     public class Assign10_Roberts{ public static void main(String[] args){   //input Scanner Scanner input=new Scanner(System.in);   int midTerm1=0;   int midTerm2=0;   int finalExam=0;     int[][]grades= new int[10][10];     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("MidTerm1 Grades "+(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("Midterm2 Grades "+(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(); }   for (int i=0;i<10;i++)   grades[i]=midterm1[i]+midterm2[i]+finalExam   int minGrade=grades[0];   int maxGrade=grades[0];   for (int i=1;i<10;i++)   { if (minGrade>grades[i])   minGrade=grades[i];   if (maxGrade<grades[i])   maxGrade=grades[i];   }   System.out.println("The minimum grade is "+minGrade);   System.out.println("The maximum grade is "+maxGrade);   } } }```

also this is cross posted, thanks
help w/ storing/scanning numbers in two dimensional arrays - Java Forums
• November 30th, 2010, 10:31 PM
copeg
Re: help w/ storing/scanning numbers in two dimensional arrays
Define "get errors all over the place"...compile time errors? Runtime errors? Post them all in full, which will help the diagnosis.
• November 30th, 2010, 11:41 PM
robertsbd
Re: help w/ storing/scanning numbers in two dimensional arrays
heres the errors im getting

```
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Duplicate local variable i
j cannot be resolved to a variable
Duplicate local variable i
j cannot be resolved to a variable
Duplicate local variable i
midterm1 cannot be resolved to a variable
midterm2 cannot be resolved to a variable
Syntax error, insert ";" to complete Statement
Type mismatch: cannot convert from int[] to int
Type mismatch: cannot convert from int[] to int
Duplicate local variable i
The operator > is undefined for the argument type(s) int, int[]
Type mismatch: cannot convert from int[] to int
The operator < is undefined for the argument type(s) int, int[]
Type mismatch: cannot convert from int[] to int

at Assign10_Roberts.main(Assign10_Roberts.java:39)
```
• December 1st, 2010, 04:49 AM
JavaPF
Re: help w/ storing/scanning numbers in two dimensional arrays
There really are all sorts of errors!

For a start, there are multiple for loops inside each other which is causing issues with duplicate variables.

I would fix these first.
• December 1st, 2010, 11:51 AM
robertsbd
Re: help w/ storing/scanning numbers in two dimensional arrays
ok here is my updated code and errors

Code java:

```  import java.util.Scanner; import java.io.*;     public class Assign10_Roberts{ public static void main(String[] args){   //input Scanner Scanner input=new Scanner(System.in);   int midTerm1=0;   int midTerm2=0;   int finalExam=0;     int[][]grades= new int[10][10];   for(int i=0; i<10; i++) { // Midterm 1 grades[i][0]=input.nextInt(); } for(int i=0; i<10; i++) { // Midterm 2 grades[i][1]=input.nextInt(); } for(int i=0; i<10; i++) { // Final Exam grades[i][2]=input.nextInt();   System.out.println("Enter the 10 Midterm 1 grades: "); grades[i][j]=input.nextInt();   System.out.print("Enter the 10 Midterm 2 grades: "); grades[i][j]=input.nextInt();   System.out.print("Enter the 10 Final Exam grades: "); grades[i][j]=input.nextInt(); }   { if (minGrade>grades[i])   minGrade=grades[i];   if (maxGrade<grades[i])   maxGrade=grades[i];   }   System.out.println("The minimum grade is "+minGrade);   System.out.println("The maximum grade is "+maxGrade);   }   }```
minGrade cannot be resolved to a variable
maxGrade cannot be resolved to a variable

at Assign10_Roberts.main(Assign10_Roberts.java:39)
• December 1st, 2010, 02:45 PM
copeg
Re: help w/ storing/scanning numbers in two dimensional arrays
Nowhere is min/maxgrade defined. They need to be defined to be used. For example
Code :

```int mingrade = 0; int maxgrade = 0; //now you can use the variables.```
Typically, since you are looking at min/max values, you want to initialize these values to their opposite extremes, for example initialize mingrade to Integer.MAX_VALUE and vice-versa for maxgrade.
• December 1st, 2010, 03:36 PM
Dejan
Re: help w/ storing/scanning numbers in two dimensional arrays
It also seems that you are trying to access grades[i] outside the scope of the for loop. So I will be undefined as well. Since it is a 2 dimensional array and you only need 3 grades and a total, for 10 students. The array should not be [10][10] but [4][10] which means you have 4 rows and 10 columns.

If you are still having trouble, let us know.

Dejan
• December 1st, 2010, 07:11 PM
robertsbd
Re: help w/ storing/scanning numbers in two dimensional arrays
here is my updated code and errors

Code java:

```public class Assign10_Roberts{ public static void main(String[] args){   //input Scanner Scanner input=new Scanner(System.in);   int midTerm1=0; int midTerm2=0; int finalExam=0; int minGrade = 0; int maxGrade = 0;   int[][]grades= new int[9][9];   for(int i=0, j=0; i<10; i++) { // Midterm 1 grades[i][0]=input.nextInt(); } for(int i=0; i<10; i++) { // Midterm 2 grades[i][1]=input.nextInt(); } for(int i=0, j=0; i<10; i++) { // Final Exam grades[i][2]=input.nextInt();   System.out.println("Enter the 10 Midterm 1 grades: "); grades[i][j]=input.nextInt();   System.out.print("Enter the 10 Midterm 2 grades: "); grades[i][j]=input.nextInt();   System.out.print("Enter the 10 Final Exam grades: "); grades[i][j]=input.nextInt();         if (minGrade>grades[i])   minGrade=grades[i];   if (maxGrade<grades[i])   maxGrade=grades[i];   }   System.out.println("The minimum grade is " + minGrade);   System.out.println("The maximum grade is "+ maxGrade);   }   }```

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The operator > is undefined for the argument type(s) int, int[]
Type mismatch: cannot convert from int[] to int
The operator < is undefined for the argument type(s) int, int[]
Type mismatch: cannot convert from int[] to int

at Assign10_Roberts.main(Assign10_Roberts.java:49)
• December 1st, 2010, 07:16 PM
copeg
Re: help w/ storing/scanning numbers in two dimensional arrays
You cannot compare an int with an array...since your grades is a 2d array, you need to specify another index
mingrade < grades[i][0] or something to that effect. Also, re-read my post above, because this code will most likely always produce a mingrade of 0.
• December 1st, 2010, 07:46 PM
robertsbd
Re: help w/ storing/scanning numbers in two dimensional arrays
not sure is this is any better here is what i changed

Code java:

```import java.util.Scanner; import java.io.*;     public class Assign10_Roberts{ public static void main(String[] args){   //input Scanner Scanner input=new Scanner(System.in);   int midTerm1=0; int midTerm2=0; int finalExam=0;     int[][]grades= new int[10][10];   for(int i=0, j=0; i<10; i++) { // Midterm 1 grades[i][0]=input.nextInt(); } for(int i=0; i<10; i++) { // Midterm 2 grades[i][1]=input.nextInt(); } for(int i=0, j=0; i<10; i++) { // Final Exam grades[i][2]=input.nextInt();   System.out.println("Enter the 10 Midterm 1 grades: "); grades[i][j]=input.nextInt();   System.out.print("Enter the 10 Midterm 2 grades: "); grades[i][j]=input.nextInt();   System.out.print("Enter the 10 Final Exam grades: "); grades[i][j]=input.nextInt(); {   public static double getCurrentMin (double [][] input) { double minGrade = input[0][0]; double maxGrade = input[0][0]; for (int row = 0; row < input.length; row++){ for (int column = 0; column < input [row].length; column++){ if (minGrade = > input[row][column]){ minGrade = input[row][column]; } }     public static double getCurrentMax (double [][] input){ double minGrade = input[0][0]; double maxGrade = input[0][0]; for (int row = 0; row < input.length; row++){ for (int column = 0; column < input [row].length; column++){ if (maxGrade < input[row][column]){ maxGrade = input[row][column];   System.out.println("The minimum grade is " + minGrade);   System.out.println("The maximum grade is "+ maxGrade);   }   }   }   }   }```

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error, insert "}" to complete Block
Syntax error, insert "}" to complete Block
Syntax error, insert "}" to complete MethodBody

at Assign10_Roberts.main(Assign10_Roberts.java:44)
• December 1st, 2010, 10:56 PM
helloworld922
Re: help w/ storing/scanning numbers in two dimensional arrays
Format your code using tabs. This will make it a lot easier for you to tell where you have missing (or misplaced) closing braces.

The general convention is for each new block (anything surrounded by curly braces) should be indented, and the closing brace should be un-indented on a newline.

For example, here's your code with the tabs inserted (I've also put comments in on where the problems with braces are)

Code Java:

```import java.util.Scanner; import java.io.*;   public class Assign10_Roberts{ public static void main(String[] args){ //input Scanner Scanner input=new Scanner(System.in);   int midTerm1=0; int midTerm2=0; int finalExam=0;   int[][]grades= new int[10][10];   for(int i=0, j=0; i<10; i++) { // Midterm 1 grades[i][0]=input.nextInt(); } for(int i=0; i<10; i++) { // Midterm 2 grades[i][1]=input.nextInt(); } for(int i=0, j=0; i<10; i++) { // Final Exam grades[i][2]=input.nextInt();   System.out.println("Enter the 10 Midterm 1 grades: "); grades[i][j]=input.nextInt();   System.out.print("Enter the 10 Midterm 2 grades: "); grades[i][j]=input.nextInt();   System.out.print("Enter the 10 Final Exam grades: "); grades[i][j]=input.nextInt(); { // missing curly braces here // did you forget to finish your main method?   public static double getCurrentMin (double [][] input) { double minGrade = input[0][0]; double maxGrade = input[0][0]; for (int row = 0; row < input.length; row++){ for (int column = 0; column < input [row].length; column++){ if (minGrade = > input[row][column]){ minGrade = input[row][column]; } } // missing curly braces here   public static double getCurrentMax (double [][] input){ double minGrade = input[0][0]; double maxGrade = input[0][0]; for (int row = 0; row < input.length; row++){ for (int column = 0; column < input [row].length; column++){ if (maxGrade < input[row][column]){ maxGrade = input[row][column]; // missing curly braces here System.out.println("The minimum grade is " + minGrade); System.out.println("The maximum grade is "+ maxGrade); } } // these are extra curly braces and should be removed } } }```