ItDoesntWorkIsUseless. In the future, please tell us what output you were expecting and what the program did instead.
I'm assuming that by the first two conditions, you mean the first two
if-statements in your code, and not the first two cases described in your assignment.
By "it's not working properly", I'm guessing that it continuously keeps asking for new input, and that input of each name also immediately follows the output of the previous name. To fix that, you need to do the following:
Instead of using scanner.hasNext() in a while-loop, you need to repeat the loop numberStrangers times.You first need to read in every stranger's name and save them somewhere. You can use an array or a List for this. Don't print them yet.After you've read in numberStrangers names, only then print all the saved names.
Some other comments:
There is no need to make your class public, so don't.Make your classes final, unless you have a good reason not to.Why did you declare that the method throws IOException? It doesn't.Please write out variable names. Use numberOfStrangers instead of numberStrangers.Why is your variable named "words" when its purpose it to hold a "name"?You can simplify the printing of a name to just: System.out.printf("Hello %s%n", name);