Home > Software design >  How do I turn a matrix input into a multidimensional array?
How do I turn a matrix input into a multidimensional array?

Time:08-11

I'm trying to figure out how to turn this type of input from scanner:

3
a b c
d e f
g h i

into this array:

String[][] arr = {{a,b,c}, {d,e,f}, {g,h i}}

The first row specifies the dimensions of the array, and the lines after are the matrix.

This is the code that I have so far:

Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
scan.nextLine();

String[][] matrix = new String[num][num];

I know I'll probably need a loop that separates each entry in the row by spaces and then add it into the first row in the array, then check for a new line repeat with each row, but I can't figure out how to implement this code. I've made the program in Python, but I can't figure out how to do this in Java.

CodePudding user response:

The below code answers your question, namely processes the input as indicated in your question which is first obtaining the [square] matrix dimension followed by separate lines where each line contains all the elements for a single row of the matrix where the elements are separated by spaces.

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

public class Matrix {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        // Number of elements in each row and each column of matrix.
        int num = scan.nextInt();
        String[][] matrix = new String[num][num];
        scan.nextLine(); // Ignore return value.
        for (int row = 0; row < num; row  ) {

            // Enter elements for single row of matrix, delimited by spaces.
            String line = scan.nextLine();

            String[] elements = line.split("\\s ");
            for (int column = 0; column < num; column  ) {
                matrix[row][column] = elements[column]; // Note: user entered values not checked.
            }
        }
        System.out.println(Arrays.deepToString(matrix));
    }
}

Output for a sample run of the above code, using the sample values from your question:

3
a b c
d e f
g h i
[[a, b, c], [d, e, f], [g, h, i]]

CodePudding user response:

Firstly, since we are taking int rather than strings, the matrix 2d-array should be one of int.

The rest should be almost identical structurally to your python code as far as I can imagine:

...
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
int[][] matrix = new int[num][num];

scan.nextLine();
for (int i = 0; i < num; i  ) {
  for (int j = 0; j < num; j  ) {
    matrix[i][j] = scan.nextInt();
  }
  scan.nextLine();
}
...

CodePudding user response:

You can solve this problem by looping through the rows and columns in a 2D array.

import java.util.*;

class Test {
    public static void main(String[] args) {
        int row = 3, column = 3;
        String[][] matrix = new String[row][column];
        
        read(matrix, row, column);
        print(matrix, row, column);
    }
    
    public static void read(String[][] matrix, int row, int column) {
        Scanner scanner = new Scanner(System.in);
        String input;

        for(int i = 0 ; i < row ;   i) {
            for(int j = 0 ; j < column ;   j) {
                input = scanner.nextLine();
                matrix[i][j] = input;
            }
        }
    }
    
    public static void print(String[][] matrix, int row, int column) {
        for(int i = 0 ; i < row;   i) {
            for(int j = 0 ; j < column ;   j) {
                String result = String.format("MATRIX [%d][%d]: %s", i, j, matrix[i][j]);
                System.out.println(result);
            }
        }
    }
}
  • Related