Inside the first loop, it is not taking the first word as an input. When I'm not taking input of the string length from the user, it's working fine. See the output where its not taking input the word 0.
public class Stringinput {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("How many words do you need to make the sentence: ");
int n = in.nextInt();
String[] names = new String[n];
for (int i = 0; i < names.length; i++) {
System.out.printf("Enter word no %d : ",i);
names[i]= in.nextLine();
}
System.out.println("The sentence you made is: ");
for (int i = 0; i < names.length; i++) {
System.out.print(names[i]+" ");
}
System.out.println("");
}
}
And this the output:
How many words do you need to make the sentence:
4
Enter word no 0 : Enter word no 1 : My
Enter word no 2 : name
Enter word no 3 : is
The sentence you made is:
My name is
BUILD SUCCESSFUL (total time: 11 seconds)