package practice;
import java.util.*;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of strings : ");
int n=sc.nextInt();
String[] str = new String[n];
System.out.println("Enter strings : ");
for(int i=0;i<n;i++) {
str[i]=sc.nextLine();
}
for(int i=0;i<n;i++) {
System.out.println(str[i]);
}
}
}
This is a simple java program to get and print strings as array. But when i ran this code, if i give number of strings as 3, only 2 strings are taking as input, instead of 3. Then it directly prints the output, which is the 2 input strings. why is this happening?