Create matrix with user input in java

Let’s learn to create matrix with user input in java.

create matrix with user input in java

A matrix is similar to table with rows and columns. Matrix can have any number of dimensions called matrices. In java most of the arrays are of single or two dimensions.

Also read – operators in java

NOTE: The datatype of matrix elements must be same.

While defining a matrix (2d array row column) we should specify that the array has more than one dimension by writing square brackets after datatype.

Declaring multidimensional array java

// declaring 2d array rows and columns

int[ ][ ] marks = new int[5][5];

Here we have to remember that when an array is created it will be filled with zero by default if it is an integer array, null values for string array and “false” for boolean.

Use of nested loops is recommended while printing 2d array in matrix form in java.

Also read – matrix multiplication in java

We are going to create a matrix in java to set the borders of matrix equal to 1 and the rest equal to 0. To do this we have to open eclipse IDE and create a new class like this,

Create matrix with user input in java – using Scanner class

Now let’s learn two dimensional array in java using scanner. In the below java program we have to read and print a two dimensional array or print matrix in java.

// how to create a matrix in java
import java.util.Scanner;

public class JavaMatrix 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in); 
      System.out.println("Please enter number of matrix rows : "); 
      int row = sc.nextInt(); 

      System.out.println("Please enter number of matrix columns : "); 
      int col = sc.nextInt();

      // defining two dimensional array java
      int numbers[][] = new int[row][col];

      // filling java matrix
      fillingMatrix(sc, numbers, row, col);

      // printing 2d array in matrix form in java
      printingMatrix(numbers, row, col);
   }

   public static void fillingMatrix(Scanner scan, int num[][], int rows, int columns)
   {
      System.out.println("Please enter elements in matrix : ");
      for(int a = 0; a < rows; a++)
      {
         for(int b = 0; b < columns; b++)
         {
            num[a][b] = scan.nextInt();
         }
      }
   }

   public static void printingMatrix(int num[][], int rows, int columns)
   {
      System.out.println("Printing 2d array in matrix form : ");
      for(int a = 0; a < rows; a++)
      {
         for(int b = 0; b < columns; b++)
         {
            System.out.print(num[a][b] + "\t");
         } 
         System.out.println();
      }
   }
}



Output :

Please enter number of matrix rows :
3
Please enter number of matrix columns :
3
Please enter elements in matrix :
10
20
30
40
50
60
70
80
90
Printing 2d array in matrix form  :
10 20 30
40 50 60
70 80 90

Java program to print 3×3 matrix

Let’s see to print 3×3 matrix in java.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Print3X3Matrix 
{
   public static void main(String[] args) throws IOException 
   {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      int[][] arrNumbers = new int[3][3];
      int a, b;
      String str;
      System.out.println("Please enter elements for 3x3 matrix: ");
      for(a = 0; a <= 2; a++)
      {
         for(b = 0; b <= 2; b++)
         {
            str = br.readLine();
            arrNumbers[a][b] = Integer.parseInt(str);
         }
      }
      System.out.println("Elements of 3x3 matrix are: ");
      System.out.println("");
      for(a = 0; a <= 2; a++)
      {
         for(b = 0; b <= 2; b++)
         {
            System.out.print(arrNumbers[a][b] + "\t");
         }
         System.out.println();
      }
   }
}


Output:

Please enter elements for 3×3 matrix:
2
4
6
8
1
3
5
7
9
Elements of 3×3 matrix are:

2 4 6
8 1 3
5 7 9

Also read – transpose of a matrix in java