trying to fill a 2D array by user-input how to do

2019-06-10 22:11发布

问题:

i am creating a code that allow user to enter his input to create a 2D array but it did not work as it should i do not know where is the problem if anyone can help me i will appreciate that.

this my code :

package test7;

import java.util.Scanner;

public class test7 {

    private int row = 4;
    private int col = 4;
    private int[][] matrix;

    public test7(int trow, int tcol) {

        this.row = trow;
        this.col = tcol;
    }

    public test7(int trow, int tcol, int[][] m) {

        this.row = trow;
        this.col = tcol;
        this.matrix = m;
    }
public int[][] fill(){


        int[][] data = new int[row][col];
        Scanner in = new Scanner(System.in);

        for(int row = 0; row< matrix.length; row++){
            for(int col = 0 ;col< matrix[row].length; col++){
                System.out.println("enter the elementss for the Matrix");
                data[row][col] = in.nextInt();


            }
            System.out.println();
        }
        return data;
    }



    public static void main(String[] args){
        int[][] ma = new int[3][2];
        test7 q2 = new test7(3, 2,ma);
        q2.fill();
    }
}

the output:

enter the elementss for the Matrix
4
enter the elementss for the Matrix
3

enter the elementss for the Matrix
5
enter the elementss for the Matrix

8

enter the elementss for the Matrix
9
enter the elementss for the Matrix
0

the output should look exactly like this:

1 2

3 4

5 6

回答1:

 public int[][] fill(){
      int[][] data = new int[row][col];  
      Scanner in = new Scanner(System.in) 

     .....

      return data; 

  } 

You were declaring data array to length

 [0][0]

This is why the error is. Change the statement to above given code.

UPDATE

   public int[][] fill(){ 
        int[][] data = new int[row][col]; 
        Scanner in = new Scanner(System.in);
        for(int row = 0; row< matrix.length; row++){ 
              for(int col = 0 ;col< matrix[row].length; col++){ 
                   System.out.println("enter the elementss for the Matrix"); 
                  data[row][col] = in.nextInt(); 
               } System.out.println(); 
          } 

           for(int row = 0; row< matrix.length; row++){
       for(int col = 0 ;col< matrix[row].length; col++){ 
             System.out.println(data[row][col]);
       } 
      System.out.println(); 
   }
         return data; 

}

This will give you the desired output , add it to fill method before the return statement



回答2:

Try this:

System.out.println("enter the elementss for the Matrix");
    Scanner in = new Scanner(System.in);
    int[][] data = new int[matrix.length][];//note the change here
    for(int i = 0; i< matrix.length; i++){
        data[i]=new int[matrix[i].length]; // note the the change here
        for(int j = 0 ;j< matrix[i].length; j++){
            data[i][j] = in.nextInt(); // note the change here
        }
        system.out.println();
    }
    return data;


回答3:

Replace your fill() method to this

   public int[][] fill(){
    int a[][]=new int[row][col];

            Scanner input = new Scanner(System.in);
System.out.println("enter the elementss for the Matrix");
    for(int row=0;row<3;row++){

        for(int col=0;col<3;col++){

            a[row][col]=input.nextInt();
            }
        }
return a;
}

Reason:

Your current fill() method is not saving values to the array you must ad this line

 a[row][col]=input.nextInt();

using

  int x = in.nextInt();
           system.out.print(x);

you are just entering the data again and again on the int variable x

Update

change this

 int[][] data = new int[0][0];

to this

 int[][] data = new int[row][col];