GETTING INPUT

SCENARIO 3B
GETTING n SPACE DELIMITED DATA PER LINE OVER m LINES (m & n are determined at run-time)

 


Note: The value of m and n are on the first line entered by the user.  For sample input 1, m is 2 (meaning there are 2 lines of data) and n is 3 (meaning each line has three pieces of data).

SAMPLE INPUT 1

               2 3
               5 2 1
               3 1 0

 

SAMPLE INPUT 2

               4 5
               2 0 3 4 0
               2 3 0 4 1
               1 2 0 4 8

               0 3 4 3 7
              


Here is an example.

 


EXAMPLE

Below is an example where the program first reads in the number of lines and the amount of data per line and then reads in all data to a 2D array.

 

import java.util.Arrays;

import java.util.Scanner;

 

public class InputBasics 

{

       public static void main(String[] args)

       {

             Scanner scr = new Scanner(System.in);

 

             int rows = scr.nextInt();  //n

             int cols = scr.nextInt();  //m

             int[][] grid = new int[rows][cols];

            

             for (int r=0; r<rows; r++)

             {

                    for (int c=0; c<cols; c++)

                    {

                           grid[r][c] = scr.nextInt();

                    }

             }

 

             //process the data array here   

             System.out.println(Arrays.deepToString(grid));

       }

}

 

 

SAMPLE EXECUTION

3 5

4 3 1 4 5

9 2 1 43 1

9 1 8 0 1

[[4, 3, 1, 4, 5], [9, 2, 1, 43, 1], [9, 1, 8, 0, 1]]