Following are the requirements..
Input: The first line of input would consist of a number N, the number of 9*9 matrix. Next N*9 lines would contain the puzzles in the specified format.
Output: A single number which is the sum of all the numbers on the main diagonals of all the given puzzles.
Following is the code I wrote:(Is the logic correct? Why doesit not produce desired output?)
import java.util.Scanner;
class TheInvitationGame{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[][][] = new int[n][9][9];
int arr[][] = new int [10][9];
int sum = 0;
String inpline[][] = new String[n][9];
for(int k=0; k<n; k++){
for(int i=0; i<9; i++){
inpline[k][i] = sc.next();
for(int j=0; j<9; j++){
a[k][i][j] = inpline[k][i].charAt(j);
if(i==j){
sum += a[k][i][j];
}
}
System.out.println();
}
}
System.out.println(sum);
}
}
Here, i wish to take 3D array [k][i][j] in which k is use to iterate NO. of 9*9 input matrices, i = line(in String format) that user will enter, j = no. of columns in matrix that will also serve as charAt() variable
Following is the stacktrace:(the 464 output is supposed to be 32(0+2+4+6+8+0+2+4+6)
1 012345678
123456789
234567890
345678901
456789012
567890123
678901234
789012345
890123456
464