Here is my code:
Scanner s = new Scanner(System.in);
int n = s.nextInt();
s.next();
String[] words = new String[n];
for ( int a = 0; a < n; a++ )
{
words[a] = s.nextLine();
}
for ( int a = 0; a < n; a++ )
{
System.out.println(words[a]);
}
The program is about inputting a number of how many lines of string you will input.
Now when I input 2, the size of the words array will become 2, on the first for loop it will ask the user to enter a string N times. after that, the second for loop will print all the inputted words from the array, but it only outputs empty strings.
Example of unexpected output:
2
hello
world
Process completed
Expected output:
2
hello
world
hello
world
Process completed
i dont know why, but when i put some System.out.print() or System.out.println() in my program it works perfectly