I want to take string inputs in java which is separated by spaces in a 2D array and display all the items in a separate line.
e.g:
input:
item1 3 5
item2 7 4
output:
item1
3
5
item2
7
4
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
String array_of_items[][] = new String[a][b];
for(int i=0;i<a;i++)
{
String str = sc.nextLine();
String[] lineVector = str.split(" ");
for(int j=0;j<b;j++)
{
array_of_items[i][j] = lineVector[j];
}
}
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
System.out.println(array_of_items[i][j]);
}
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1