How to read a Matrix from user in Java? Last Updated : 05 Mar, 2019 Comments Improve Suggest changes Like Article Like Report Given task is to read a matrix from the user. The size and number of elements of matrices are to be read from the keyboard. Java // Java program to read a matrix from user import java.util.Scanner; public class MatrixFromUser { // Function to read matrix public static void readMatrixByUser() { int m, n, i, j; Scanner in = null; try { in = new Scanner(System.in); System.out.println("Enter the number " + "of rows of the matrix"); m = in.nextInt(); System.out.println("Enter the number " + "of columns of the matrix"); n = in.nextInt(); // Declare the matrix int first[][] = new int[m][n]; // Read the matrix values System.out.println("Enter the elements of the matrix"); for (i = 0; i < m; i++) for (j = 0; j < n; j++) first[i][j] = in.nextInt(); // Display the elements of the matrix System.out.println("Elements of the matrix are"); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) System.out.print(first[i][j] + " "); System.out.println(); } } catch (Exception e) { } finally { in.close(); } } // Driver code public static void main(String[] args) { readMatrixByUser(); } } Output: Enter the number of rows of the matrix 2 Enter the number of columns of the matrix 2 Enter the elements of the matrix 1 2 3 4 Elements of the matrix are 1 2 3 4 Comment More infoAdvertise with us Next Article How to read a Matrix from user in Java? D DeepakRathi Follow Improve Article Tags : Java Practice Tags : Java Similar Reads How to Select Random Rows from a Matrix in MATLAB? A matrix is an n x n array that stores integers, floating point numbers or alphanumeric data in MATLAB. Indexing a matrix is the same as indexing an array.  Syntax:matrix_name(i,j)where, i is the row number, and  J is the column number which is to be indexed. Example 1: Matlab % MATLAB code for sel 2 min read Java Program to Print Matrix in Z form Given a square matrix of order n*n, we need to print elements of the matrix in Z form Examples: Input : mat[][] = {1, 2, 3, 4, 5, 6, 7, 8, 9} Output : 1 2 3 5 7 8 9 Input : mat[][] = {5, 19, 8, 7, 4, 1, 14, 8, 2, 20, 1, 9, 1, 2, 55, 4} Output: 5 19 8 7 14 20 1 2 55 4 Java // Java program to print a 2 min read How to Create a Matrix From a Nested Loop in MATLAB? Matrices are 2-dimensional arrays that store numeric or symbolic data. It is convenient to create them with the help of nested loops as the outer loop creates the elements along one dimension and the inner loop creates the elements along the second dimension. In this article, we will see how to crea 2 min read Ways to Read Input from Console in Java In Java, there are four different ways to read input from the user in the command line environment(console). 1. Using Buffered Reader ClassBuffered Reader Class is the classical method to take input, Introduced in JDK 1.0. This method is used by wrapping the System.in (standard input stream) in an I 5 min read Graph Adjacency Matrix in Java A graph is a type of data structure used to represent the relationship between the entities. In this article, we will learn to represent a graph in the form of Adjacency Matrix. Graph Adjacency MatrixThe Adjacency matrix is the way to represent the graphs using the 2D array. It is the fundamental da 6 min read Like