Split into Words
Completed
Read a line of words separated by one or more spaces. Print the number of words and then each word on its own line.
Input
One line.
Output
The count, then the words.
Example
Input
red green blue
Output
3 red green blue
Editor
Java
Hints
split("\\s+") splits on runs of whitespace.
trim() first so leading spaces don't create an empty word.
Solution explanation
split takes a regular expression; \\s+ means one or more whitespace characters.
Show solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] words = sc.nextLine().trim().split("\\s+");
System.out.println(words.length);
for (String w : words) {
System.out.println(w);
}
}
}