create matrix with user input in java

Solutions on MaxInterview for create matrix with user input in java by the best coders in the world

showing results for - "create matrix with user input in java"
Olivia
12 Oct 2019
1import java.util.Scanner;
2public class MatrixUserInput
3{
4   public static void main(String[] args)
5   {
6      Scanner sc = new Scanner(System.in);
7      System.out.println("Please enter number of matrix rows : ");
8      int row = sc.nextInt();
9      System.out.println("Please enter number of matrix columns : ");
10      int col = sc.nextInt();
11      // defining two dimensional array java
12      int[][] numbers = new int[row][col];
13      // filling java matrix
14      fillingMatrix(sc, numbers, row, col);
15      // printing 2d array in matrix form in java
16      printingMatrix(numbers, row, col);
17   }
18   public static void fillingMatrix(Scanner scan, int num[][], int rows, int columns)
19   {
20      System.out.println("Please enter elements in matrix : ");
21      for(int a = 0; a < rows; a++)
22      {
23         for(int b = 0; b < columns; b++)
24         {
25            num[a][b] = scan.nextInt();
26         }
27      }
28   }
29   public static void printingMatrix(int num[][], int rows, int columns)
30   {
31      System.out.println("Printing 2d array in matrix form : ");
32      for(int a = 0; a < rows; a++)
33      {
34         for(int b = 0; b < columns; b++)
35         {
36            System.out.print(num[a][b] + "\t");
37         }
38         System.out.println();
39      }
40   }
41}