How to take n spaced input for multiple lines in java? [closed]

Tags:



11 22 33

44 54 63

73 53 24

I am unable to take input in this format in java. I am comfortable with n spaced input for single line but am unable to take n spaced multiple line input used in competitive coding. Can anyone provide me an easy solution to it.

Answer

You can use a Scanner for this purpose:

import java.util.Scanner;
public class GetInput {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int[][] numbers = new int[3][3];

        for(int i = 0; i < 3; i++) {    //Iterating over the lines - in this case the user has to enter 3 lines
            for(int j = 0; j < 3; j++) {    //User has to enter 3 numbers per line
                numbers[i][j] = scan.nextInt(); //Scans the next int. It is not a problem that you don't press enter after each number
            }
        }

        //Output - just to test if all went correct
        for(int i = 0; i < 3; i++) {
            for(int j = 0; j < 3; j++) {
                System.out.print(numbers[i][j] + " ");
            }
            System.out.println("");
        }

    }
}


Source: stackoverflow