Read the API docs for Scanner (and java.util.regex.Pattern and Matcher) - it can use regular expressions to cope with variable-sized inputs and delimiting space.