Pregunta

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?

¿Fue útil?

Solución

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]+"))

Otros consejos

Try this:

if (tokens[i].matches([0-9]+(\.[0-9][0-9]?)?)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top