Question

if (tokens[i].matches("[0-9]+"))

This makes it currently only read whole integers until a space occurs, how do I make it read decimals as well?

Was it helpful?

Solution

Add an optional decimal part:

if (tokens[i].matches("[0-9]*([.][0-9]+)?"))

Use asterisk to make the integer portion optional. The problem with this expression, however, that now it could match an empty string. A better (but longer) expression would match a string that starts in a decimal point in a separate subexpression, as follows:

if (tokens[i].matches("[0-9]+([.][0-9]+)?|[.][0-9]+"))

OTHER TIPS

Try this:

if (tokens[i].matches([0-9]+(\.[0-9][0-9]?)?)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top