I am trying to read the user input from the console directly into an array list, separated by each word or entity that has whitespace on either side (the default behavior). The problem is I am stuck inside of a while loop. Scanner.next() keeps waiting for more input, though I know I want to stop feeding input after the user presses return:
Scanner input = new Scanner(System.in);
System.out.print("Enter a sentence: ");
do {
if (words.add(input.next());
} while (input.hasNext());
System.out.println(words);
I input a line, except I will have to press CTRL-D to terminate it (IE. signal the end of input). I just want to automatically end it when the return ‘\n’ character is pressed at the end of the sentence.
It may be simpler to use split for what you want.