Sunarto
Sunarto

Reputation: 37

Java Input scanner to array multidimensional

I want to display an array of input that I input. and in print automatically. I want to display the array of input values ​​that I input. and will be printed automatically.

my code like this :

public class ReadArray {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Input total row : ");
int row = sc.nextInt();
System.out.print("Input total column : ");
int column = sc.nextInt();

int [][] matrix = new int[row][column];
for (int i = 0; i < row; i++)
{
    for(int j = 0; j < column; j++) {
        System.out.println("Row ["+i+"]:  Column "+j+" :");
    matrix[i][j] = sc.nextInt(); 
}

}



    }

}

I want results like this :

Input total row : 2 Input total column : 2

Row [0]: Column 0 : 1

Row [0]: Column 1 : 2

Row [1]: Column 0 : 10

Row [1]: Column 1 : 11

Data Array 1 : 1,2 Data Array 2 : 10,11

anyone can help me please.

Upvotes: 0

Views: 39911

Answers (4)

Sonu patel
Sonu patel

Reputation: 339

  • Print the row and col number and then the entre the matix data and print it in matrix form

    Scanner scan = new Scanner(System.in);

        System.out.println("Enter The Number Of Matrix Rows");
    
        int row = scan.nextInt();
    
        System.out.println("Enter The Number Of Matrix Columns");
    
        int col = scan.nextInt();
    
        //defining 2D array to hold matrix data
        int[][] matrix = new int[row][col];
        // Enter Matrix Data
        enterMatrixData(scan, matrix, row, col);
    
        // Print Matrix Data
        printMatrixData(matrix, row, col);
    

    } public static void enterMatrixData(Scanner scan, int[][] matrix, int row, int col){

     System.out.println("Enter Matrix Data");
    
          for (int i = 0; i < row; i++)
          {
              for (int j = 0; j < col; j++)
              {
                  matrix[i][j] = scan.nextInt();
              }
          }
    

    }

    public static void printMatrixData(int[][] matrix, int row, int col){

    System.out.println("Your Matrix is : ");

        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                System.out.print(matrix[i][j]+"\t");
            }
    
            System.out.println();
        }       
    

    }

Upvotes: 0

Alya&#39;a Gamal
Alya&#39;a Gamal

Reputation: 5638

   String result="";//this variable for the last line which print the result
   for (int i = 0; i < row; i++) {
     result=result+"Data Array "+i+" :";
       for (int j = 0; j < column; j++) {
         System.out.println("Row [" + i + "]:  Column " + j + " :");
         matrix[i][j] = sc.nextInt();
         result=result+matrix[i][j]+", ";

        }

    }
System.out.println(result);////for the final result

Upvotes: 4

Shivam
Shivam

Reputation: 714

The code is as follows

        for (int i = 0; i < baris; i++)
        {
            for(int j = 0; j < column; j++) {

    // print array data to screen
                System.out.println("Data Array "+(i+1) +matrix[i][j]);

        }
System.out.println();
}

I hope this code is help you so Please review it .

Upvotes: 0

Achintya Jha
Achintya Jha

Reputation: 12843

for(int j = 0; j < column; j++) {
    System.out.println("Row ["+i+"]:  Column "+j+" :");
    matrix[i][j] = sc.nextInt(); //Storing input value here    
    System.out.println(matrix[i][j]);//Output the input value
}

Upvotes: 0

Related Questions