Help me with this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Help me with this code?

import java.io.*; class Matrix { public static void main(String args[]) { int n,i,j,k; BufferedReader br=new BufferedReader(new InputStreamReader(System.in))throws IOException System.out.println("enter n value"+n); n=Integer.parseInt(br.readLine()); System.out.println("enter the matrix a"); for(i=0;i<n;i++) { for(j=0;j<n;j++) int a[i][j]=Integer.parseInt(br.readLine ()); } System.out.println("enter the matrix b"); for(i=0;i<n;i++) { for(j=0;j<n;j++) int b[i][j]=Integer.parseInt(br.readLine()); } for(i=0;i<n;i++) { for(j=0;j<n;j++) System.out.println("Addition is:"+(a[i][j]+b[i][j])); } } }

9th Mar 2022, 1:53 PM
Madhu Rampelli
Madhu Rampelli - avatar
1 Answer
+ 2
import java.io.*; class Matrix { public static void main(String args[]) throws IOException //method can throws , not statements { int n,i,j,k; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter n value :"); //n is uninitialized so you cant use here n=Integer.parseInt(br.readLine()); //declare array here, in loop its wrong int a[][] = new int[n][n]; int b[][] = new int[n][n]; System.out.println("enter the matrix a"); for(i=0;i<n;i++) { for(j=0;j<n;j++) a[i][j]=Integer.parseInt(br.readLine()); } System.out.println("enter the matrix b"); for(i=0;i<n;i++) { for(j=0;j<n;j++) b[i][j]=Integer.parseInt(br.readLine()); } for(i=0;i<n;i++) { for(j=0;j<n;j++) System.out.println("Addition is:"+(a[i][j]+b[i][j])); } } }
9th Mar 2022, 2:05 PM
Jayakrishna 🇮🇳