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

In the below java program first user enters number of rows and columns as input using nextInt() method of Scanner class.

Also read – matrix multiplication in java

This input is stored in two integer variables ‘row’ and ‘col’.

In the next step two dimensional array is defined with ‘row’ and ‘col’ values.

Then using fillingMatrix() and printingMatrix() methods matrix is filled with user input and finally printed on the console.

Here’s the example on matrix with user input or two dimensional array in java user input

import java.util.Scanner;
public class MatrixUserInput
{
   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


two dimensional array in java using scanner

In the below java program user enters row and column length of an array using nextInt() method of Scanner class.

Then two dimensional array is declared with row and column values.

In the next step two for loops are used to store input values entered by user and to print array on console.

Here’s the program on two dimensional array in java using scanner.

import java.util.Scanner;
public class TwoDimensionalArrayUsingScanner
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter row length: ");
      int row = sc.nextInt();
      System.out.println("Please enter column length: ");
      int column = sc.nextInt();
      int[][] arrNumbers = new int[row][column];
      System.out.print("Please enter " + row * column + " elements:\n");
      for(int a = 0; a < row; a++)
      {
         for(int b = 0; b < column; b++)
         {
            arrNumbers[a][b] = sc.nextInt();
         }
      }
      System.out.print("The elements are:\n");
      for(int a = 0; a < row; a++)
      {
         for(int b = 0; b < column; b++)
         {
            System.out.println("Row [" + a + "]: Column [" + b + "]: " + arrNumbers[a][b]);
         }
      }
      sc.close();
   }
}


Output:

Please enter row length:
2
Please enter column length:
3
Please enter 6 elements:
1
3
5
2
4
6
The elements are:
Row [0]: Column [0]: 1
Row [0]: Column [1]: 3
Row [0]: Column [2]: 5
Row [1]: Column [0]: 2
Row [1]: Column [1]: 4
Row [1]: Column [2]: 6


So this is all about create matrix with user input in java. I hope you have understood the concept.

Create matrix with user input is the most asked interview question.

I hope this post will be helpful for java freshers.