I came across this question and want to recreate it but fill it with an array of strings instead of integers. I want to use arrays and not ArrayList just because I am a beginner and would like to practice more with arrays. I pretty much copied the code but I keep getting an error in the output. Here is my code:
Scanner input = new Scanner(System.in);
System.out.print("Enter number of arrays: ");
int x = input.nextInt();
String [][] array = new String[x][0];
for(int i = 0; i < x; i++){
System.out.print("Enter number of elements for array: ");
int s = input.nextInt();
array[i] = new String[s];
for(int j = 0; j < s ; j++){
System.out.print("Enter string: ");
String word = input.nextLine();
array[i][j] = word;
}
}
My output is:
Enter number of arrays: 2
Enter number of elements for array: 3
Enter string: Enter string: hello
Enter string: hi
Enter number of elements for array: 2
Enter string: Enter string: goodbye
Why does it print "Enter string" twice every time? The logic makes sense to me so I'm not sure whats causing the wrong output. Is it the for loop or just the way strings work? Explanation and help with the code would be appreciated. Thanks