00 Votes

Java: Check if String only contains Numbers

Question by Guest | 2016-08-04 at 23:44

Is is there any possibility in Java to determine whether a string only contains numbers (digits from 0 to 9)?

I would like to test some user input before further processing and to show an error message if necessary.

ReplyPositiveNegative
0Best Answer0 Votes

You can just use the function .isNumeric(). A string can be passed to this function and you get TRUE or FALSE as a result depending on the content of the string:

if (StringUtils.isNumeric("123")) {  // true

}

if (StringUtils.isNumeric("ABC")) {  // false

}

if (StringUtils.isNumeric("1 3")) {  // false

}

In this example, accordingly, only "123" would result in TRUE because the strings "ABC" and "1 3" are not only consisting of digits.
2016-08-05 at 13:04

ReplyPositive Negative
Reply

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.