Is it possible to create an array with different sizes of columns using Java scanner? like = [[1, 2][3, 4, 5, 6][7, 8, 9]] I've tried it but It doesn't work when the variable element is outside the loop next to it, but when it's inside the loop next to it, it seems working. Here's the code:
import java.util.Scanner;
/**
*
* @author cismvillanueva
*/
public class FindInGroup {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StringBuilder output = new StringBuilder();
System.out.print("Enter number of groups: ");
int groups = scanner.nextInt(); //number of groups
System.out.print("Enter number of elements to be search: ");
int nSearch = scanner.nextInt();
int[] nElements = new int[groups];
for (int i = 0; i < groups; i++) {
System.out.print("Enter number of elements for each group: ");
nElements[i] = scanner.nextInt();
}
int[][] elements = new int[groups][nElements[i]];//here's the variable that gives error when outside the next loop
for (int i = 0; i < groups; i++) {
for (int j = 0; j < nElements[i]; j++) {
System.out.print("Enter number: ");
elements[i][j] = scanner.nextInt();
output.append("\n").append(elements[i][j]);
}
System.out.println(" ");
}
}
}