I’m trying to get Java’s scanner to keep accepting input until the user types some variation of “end,” like, “END”, “eNd”, “eND”, etc.
However, due to my limited experience with Scanner, I can’t think of code that would work for all variations of “end”. Here’s what I have so far:
private static Scanner userInput = new Scanner(System.in);
String textBody = "";
while(!userInput.hasNext("end") && !userInput.hasNext("END"))
{
textBody += userInput.nextLine() + "\n";
}
Thanks for any help
If you want to terminate the loop when the user enters a line that consists of “end” (in some variation of capitals, but not terminate when the user enters “end of the road”, then try something like this:
(You could write this as a
forloop equally well.)