help me plz

The program does not sum the elements of the 2 matrix

this my code

package javaapplication143;

import java.util.*;

public class JavaApplication143 {
public static void ReadMatrix(int [][]a1 ){
     int val=0;
    Scanner input=new Scanner(System.in);
    System.out.println("Enter 3 rows and 3 Columns for matrix 1 ");
    
    for(int i=0 ; i <3 ; i++){
        for(int j=0 ; j <3 ; j++){
            val=input.nextInt(); 
        }
    }
    
}
public static void ReadMatrix2( int [][]b1 ){
     int val2=0;
    Scanner input=new Scanner(System.in);
    System.out.println("Enter 3 rows and 3 Columns for matrix 2 ");
    
    for(int i=0 ; i <3 ; i++){
        for(int j=0 ; j <3 ; j++){
            val2=input.nextInt(); 
        }
    }
   
}
public static void sumMatrix(int [][]a , int [][]b ,  int[][] sum ){
  

    System.out.println("Sum of entered matrices:-");
 
      for ( int i = 0 ; i < 3 ; i++ ){
      {
         for ( int j = 0 ;j < 3 ; j++ ){
               sum[i][j]=a[i][j]+ b[i][j];
            System.out.print(sum[i][j]+"\t");
         }
      }
         System.out.println();
      }
  
    
   
   
}
public static void main(String[] args) {
int [][]a=new int[3][3];
int [][]b=new int[3][3];
int [][]sum=new int[3][3];



ReadMatrix(a) ;
ReadMatrix2(b) ;

	

 sumMatrix(a,b,sum);
 
}
}

and this the answer

Enter 3 rows and 3 Columns for matrix 1 
1
2
3
4
5
6
7
8
9
Enter 3 rows and 3 Columns for matrix 2 
1
2
3
4
5
6
7
8
9
Sum of entered matrices:-
0	0	0	
0	0	0	
0	0	0

Can you post what the correct answer should be?
Try debugging your code by adding println statements to show the values of all the variables that are used, print them out every time their values change. Use an id String with the print out so you know what is printing:
System.out.println("varname=" + varname);

Use the Arrays class's toString() method to format the arrays for printing.

Can you post what the correct answer should be?
Try debugging your code by adding println statements to show the values of all the variables that are used, print them out every time their values change. Use an id String with the print out so you know what is printing:
System.out.println("varname=" + varname);

Use the Arrays class's toString() method to format the arrays for printing.

i did not understand you

Can you explain more?

What part don't you understand?
1) post what the correct answer should be?
2)adding println statements to show the values of all the variables that are used
I gave an example: System.out.println("varname=" + varname);
Replace varname with the variables you use in your program.
3)Use the Arrays class's toString() method to format the arrays for printing.
For a two dim array use the deepToString() method:
System.out.println("2a=" + java.util.Arrays.deepToString(a));

the answer should be like that :
matrix 1 =
[7 0 0]
[0 5 0]
[0 0 6]

+

matrix 2 =
[7 1 1]
[1 5 1]
[1 1 6]

=
answer =
[14 1 1]
[1 10 1]
[1 1 12]

At line 13 and 25, your not assigning values to your arrays indexes but just to variables you never used, try printing out the values of a1[j] and b1[j] at the next line to see what we mean

At line 13 and 25, your not assigning values to your arrays indexes but just to variables you never used, try printing out the values of a1[j] and b1[j] at the next line to see what we mean

package javaapplication143;

import java.util.*;

public class JavaApplication143 {
public static void ReadMatrix(int [][]a1 ){
     int val=0;
    Scanner input=new Scanner(System.in);
    System.out.println("Enter 3 rows and 3 Columns for matrix 1 ");
    
    for(int i=0 ; i <3 ; i++){
        for(int j=0 ; j <3 ; j++){
            val=input.nextInt(); 
             System.out.println(a1[i][j]);
        }
       
    }
    
}
public static void ReadMatrix2( int [][]b1 ){
     int val=0;
    Scanner input=new Scanner(System.in);
    System.out.println("Enter 3 rows and 3 Columns for matrix 2 ");
    
    for(int i=0 ; i <3 ; i++){
        for(int j=0 ; j <3 ; j++){
            val=input.nextInt(); 
              System.out.println(b1[i][j]);
        }
    }
   
}
public static int [][] sumMatrix(int [][]a , int [][]b ,  int[][] sum ){
  

    System.out.println("Sum of entered matrices:-");
 
      for ( int i = 0 ; i < 3 ; i++ ){
      {
         for ( int j = 0 ;j < 3 ; j++ ){
               sum[i][j]=a[i][j]+ b[i][j];
            System.out.print(sum[i][j]+"\t");
         }
      }
         System.out.println();
      }
  return sum;
    
   
   
}
public static void main(String[] args) {
int [][]a=new int[3][3];
int [][]b=new int[3][3];
int [][]sum=new int[3][3];



ReadMatrix(a) ;
ReadMatrix2(b) ;

	

 sumMatrix(a,b,sum);
 
}
}

the answer after Correction

Enter 3 rows and 3 Columns for matrix 1 
1
0
2
0
3
0
4
0
5
0
6
0
7
0
8
0
9
0
Enter 3 rows and 3 Columns for matrix 2 
1
0
2
0
3
0
4
0
5
0
6
0
7
0
8
0
9
0
Sum of entered matrices:-
0	0	0	
0	0	0	
0	0	0

okay...as you can see there your not assigning values to the arrays(shown by the 0s),
assign values to a1 and b1(with their respective indexes using the loops) instead of the variables val and val2

okay...as you can see there your not assigning values to the arrays(shown by the 0s),
assign values to a1 and b1(with their respective indexes using the loops) instead of the variables val and val2

Can you explain more?

okay...as you can see there your not assigning values to the arrays(shown by the 0s),
assign values to a1 and b1(with their respective indexes using the loops) instead of the variables val and val2

it's work now thank you both for help me

assign values to a1[j] in the loop and b1[j] in the other

Edit: glad that's solved you mark this as solved then

Did you see this suggestion I made. Your printed numbers have no meaning without an identifying String.
Use an id String with the print out so you know what is printing:
System.out.println("varname=" + varname);

Where in your code do you assign any values to the elements of the arrays?

If it has been solved then please MARK it as solved. Thanks! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.