I am a beginner to coding. Currently I am learning arrays.
In the following code, I am trying to display the words entered by the user using a String array. The code is displaying null when using enhanced for-loop. Can anyone explain the problem?
The code is working fine with normal for-loop.
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String word;
int count = 0;
String[] words = new String[100];
System.out.println("Enter the words (enter 'done' when finished):");
do {
System.out.println("Enter word "+ (count+1));
word=scanner.nextLine();
if (!word.equalsIgnoreCase("done")) {
words[count]=word;
count++;
}
}while (!word.equalsIgnoreCase("done"));
System.out.println("Words entered are:");
for (String print:words){
System.out.println(print);
}
scanner.close();
}
The code should display the words entered by the user but it is displaying null instead of the word.