In java I want to create and print a ragged array , The first thing the user must enter the number of rows and then in each row the user will have to enter the number columns and then put whatever number he wants in each column until he reaches to the number that he entered for each row , For example
4 11 22 33 44
2 51 8
6 92 1 3 5 3 99
in the first row the user entered 4 so he could type four numbers
in the second row the user entered 2 so he could type two numbers
in the third row the user entered 6 so he could type four numbers
after that it should print the number that the user entered for each row and whatever he typed in (it should print the example above)
but for some reason using my code
int rows , col, m=0 , z=0 ;
System.out.println("enter number of rows");
rows=input.nextInt();
while(z<rows){
System.out.println("in each row enter number of coloms and then enter whatever number you want in there");
col=input.nextInt();
z++;
for(int i =0 ; i<col ; i++) {
m=input.nextInt();}
}
int [][] test1 = new int [rows][m];
for(int i =0 ; i<test1.length ; i++) {
for( int j =0 ; j<test1[i].length ; j++)
System.out.print(test1[i][j]+ " ");
System.out.println();}
all of the output is zeros but the number of rows that the user entered the first thing is correct so I don't have a problem with that
So instead of having an output like this
enter number of rows
3
in each row enter number of coloms and then enter whatever number you want in there
4 11 22 33 44 // for example the user will enter these numbers and it will be printed the way he typed it
2 51 8
6 92 1 3 5 3 99
but I get this output
enter number of rows
3
4 11 22 33 44 // if the user have entered these numbers it will print all of the array zeros depending on the first number in the last row
2 51 8
6 92 1 3 5 3 99
// this is what I get
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
I have been searching to find a solution all the day but I didn't find anything , Any one knows how to solve this problem?
sorry for making you read all that