문제

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?

도움이 되었습니까?

해결책

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

다른 팁

Try this:

if (tokens[i].matches([0-9]+(\.[0-9][0-9]?)?)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top