How to Read a Matrix from User in Java?

In this post, you will see how to Read a Matrix from the user in Java. Generally, a Matrix is a 2D array. The 2D array is organized as matrices which can be represented as a collection of rows and columns.

Here we are going to read a 3×3 size Matrix using java.lang.Scanner class.

Example-1. Create Matrix With User Input Using Java

In this example, we read a Matrix value from the user using the Scanner class in java. As we know matrix is a 2D array so we will use two for loop here, one inside another. let’s see the java code for it below:

package com.javacodepoint.array.multidimensional;

import java.util.Arrays;
import java.util.Scanner;

public class UserInput2DArray {

	public static void main(String[] args) {
		// Creating scanner object for reading user input
		Scanner sc = new Scanner(System.in);

		// Declaring a 3x3 matrix
		int matrix[][] = new int[3][3];

		// Read matrix value from user
		System.out.println("Enter the elements for 3x3 Matrix: ");
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				System.out.print("Enter value for [" + i + "][" + j + "]= ");
				matrix[i][j] = sc.nextInt();
			}
		}

		// Print the Matrix
		System.out.println("The given matrix:");
		System.out.println(Arrays.deepToString(matrix));
	}

}

OUTPUT:

Enter the elements for 3×3 Matrix:
Enter value for [0][0]= 3
Enter value for [0][1]= 4
Enter value for [0][2]= 6
Enter value for [1][0]= 7
Enter value for [1][1]= 6
Enter value for [1][2]= 7
Enter value for [2][0]= 8
Enter value for [2][1]= 9
Enter value for [2][2]= 9
The given matrix:
[[3, 4, 6], [7, 6, 7], [8, 9, 9]]

Example-2. Read Matrix from the user and Display using for loop in Java

package com.javacodepoint.array.multidimensional;

import java.util.Arrays;
import java.util.Scanner;

public class UserInput2DArray2 {

	public static void main(String[] args) {
		// Creating scanner object for reading user input
		Scanner sc = new Scanner(System.in);

		// Declaring a 3x3 matrix
		int matrix[][] = new int[3][3];

		// Read matrix value from user
		System.out.println("Enter the elements for 3x3 Matrix: ");
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				System.out.print("Enter value for [" + i + "][" + j + "]= ");
				matrix[i][j] = sc.nextInt();
			}
		}

		// Print the Matrix
		System.out.println("The given matrix:");
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				System.out.print(matrix[i][j] + " ");
			}

			// Move cursor to next line
			System.out.println();
		}
	}

}

OUTPUT:

Enter the elements for 3×3 Matrix:
Enter value for [0][0]= 3
Enter value for [0][1]= 4
Enter value for [0][2]= 6
Enter value for [1][0]= 1
Enter value for [1][1]= 2
Enter value for [1][2]= 3
Enter value for [2][0]= 5
Enter value for [2][1]= 7
Enter value for [2][2]= 9
The given matrix:
3 4 6
1 2 3
5 7 9

See also:
Java Program to Find the Sum of an Array.
Java Program to Count the Number of Vowels in a String.

Java logical programs list


Java Basic Programs

Java Programs based on the Collection Framework

Leave a Reply

Your email address will not be published. Required fields are marked *