Your problem is not with split
but with Scanner
, which by default uses one or more continues whitespaces as delimiter, so s1.next()
will return only one word from user input (with no whitespaces) which means
String toParse = s1.next();
for input like This is a text
will become This
(so there are no whitespaces to split on)
If you want to read entire data from user input use s1.nextLine()
.