Before you start writing code, you need to know how to multiply two matrices and what are the conditions to do so.
Consider these images:
Matrix Multiplication in Java
Multiplication is applicable, because the number of Columns (Matrix 1) == the number of Rows (Matrix 2).
Matrix Multiplication in Java
Multiplication is not applicable, because the number of Columns (Matrix 1) != the number of Rows (Matrix 2). Finding the order of Result Matrix (Rows x Columns)
Assume that the first matrix has 4 rows & 2 columns (4 x 2) and the second matrix has 2 rows and 3 columns (2 x 3). Now, when these two matrices are multiplied, the result matrix of order (4 x 3) is produced. How to multiply two matrices
Matrix Multiplication in Java
The image shown above shows the multiplication process of two matrices. What does this program do?
This program is a demonstration of Matrix Multiplication in Java. The order of both matrices and elements in each matrix are inserted by the user. The order of matrix determines the possible number of elements in the matrix. For example, A matrix of 4 rows and 4 columns contains 16 elements and these elements are reside in the array as shown in the following image:
Matrix Multiplication in Java
Code
  1. package matrixmultiplication;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. public class MatrixMultiplication
  6. {
  7. public static void main(String[] args) throws IOException
  8. {
  9. //BufferedReader to read text from character-InputStream and to buferring characters to provide efficient reading of characters
  10. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  11. String temp;
  12. System.out.println("Insert rows in Matrix 1 >> ");
  13. //Reading the user input and copy into temp
  14. temp = br.readLine();
  15. //Assigning the value of temp to integer variable M1Row (After parsing string as signed decimal integer)
  16. int M1Row = Integer.parseInt(temp);
  17. System.out.println("Insert columns in Matrix 1 >> ");
  18. temp = br.readLine();
  19. int M1Col = Integer.parseInt(temp);
  20. System.out.println("Insert rows in Matrix 2 >> ");
  21. temp = br.readLine();
  22. int M2Row = Integer.parseInt(temp);
  23. System.out.println("Insert columns in Matrix 2 >> ");
  24. temp = br.readLine();
  25. int M2Col = Integer.parseInt(temp);
  26. //Checking whether the multiplication is applicable
  27. if (M1Col == M2Row)
  28. {
  29. //Creating and initializing arrays to hold matrices
  30. int[][] Matrix1 = new int[M1Row][M1Col];
  31. int[][] Matrix2 = new int[M2Row][M2Col];
  32. int[][] Matrix3 = new int[M1Row][M2Col];
  33. //Inserting values in Martix1
  34. System.out.println("Insert values in Matrix1:");
  35. for (int i = 0; i < M1Row; i++)
  36. {
  37. for (int j = 0; j < M1Col; j++)
  38. {
  39. temp = br.readLine();
  40. Matrix1[i][j] = Integer.parseInt(temp);
  41. }
  42. }
  43. //Inserting values in Martix2
  44. System.out.println("Insert values in Matrix2:");
  45. for (int i = 0; i < M2Row; i++)
  46. {
  47. for (int j = 0; j < M2Col; j++)
  48. {
  49. temp = br.readLine();
  50. Matrix2[i][j] = Integer.parseInt(temp);
  51. }
  52. }
  53. //Multiplying two matrices
  54. System.out.println("Multiplying...");
  55. int temp1 = 0;
  56. for (int i = 0; i < M1Row; i++)
  57. {
  58. for (int j = 0; j < M2Col; j++)
  59. {
  60. for (int k = 0; k < M2Row; k++)
  61. {
  62. temp1 = temp1 + Matrix1[i][k] * Matrix2[k][j];
  63. }
  64. //Inserting elements in result matrix
  65. Matrix3[i][j] = temp1;
  66. temp1 = 0;
  67. }
  68. }
  69. //Display result of Multiplication
  70. System.out.println("Multiplication Completed!");
  71. System.out.println("............................................");
  72. System.out.println("Result");
  73. for (int i = 0; i < M1Row; i++)
  74. {
  75. for (int j = 0; j < M2Col; j++)
  76. {
  77. System.out.print(Matrix3[i][j] + "\t");
  78. }
  79. System.out.println();
  80. }
  81. System.out.println("............................................");
  82. }
  83. else
  84. {
  85. System.out.println("Multiplication could not be done!");
  86. System.out.println("Tip: Number of columns in a matrix1 should be equal to number of rows in matrix2 to perform multiplication.");
  87. }
  88. }
  89. }
Output in NetBeans IDE 7.0
Matrix Multiplication in Java
Matrix Multiplication in Java

Output in Command Prompt
Matrix Multiplication in Java
Matrix Multiplication in Java