0

How to determine a 2D-array dimension by input two integers separated by space? My method is use split function, convert string to integer and determine 2D array. Here is my code, however there is an syntax error.

Thanks

import java.util.Scanner;
public class inputtwointeger {
private static Scanner input;
public static void main(String[] args) {
input = new Scanner(System.in);
System.out.println("Enter integer separated by spaces: ");
String firstInput = input.nextLine();
String [] secondInput = firstInput.split(" ");
System.out.println("Numbers");
for(String n: secondInput)
{
    System.out.print(firstInput+" "+secondInput);
}
//covert string into integer
int row = Integer.parseInt(secondInput[0]);
int column = Integer.parseInt(secondInput[1]);
// new a 2D array using two integer you input
int [][] array = new int[row][column]; 
}

//generate 2D array element in hand step by step
 for (int i = 0; i < row; i++) { 
        for (int j = 0; j < column; j++) { 
            array[row][column] = input.nextInt(); 
            System.out.print(Arrays.deepToString(array)); 
            } 
        System.out.println();
        }

}

Eric Wang
  • 11
  • 1
  • What is the error and where? – Guy Mar 15 '16 at 10:14
  • 1
    It's not a *syntax error*, you're not printing the content of the array. – Maroun Mar 15 '16 at 10:17
  • After running program, it show the message below: "Exception in thread "main" 3 3 [Ljava.lang.String;@265145773 3 [Ljava.lang.String;@26514577java.lang.ArrayIndexOutOfBoundsException: 3 at inputtwointeger.main(inputtwointeger.java:26)" I don't know how to fix it. – Eric Wang Mar 15 '16 at 11:31

0 Answers0