I tried with below examples, but both of them returned me false as result.
Edit: due to some confusion in other answers, I am writing a test case and will explain some more things in detail.
Although the number of digits is limited this actually creates a variable of the data which can be used, which is, I would imagine, the most common use-case. Such as you want to actually use the data in your code, not just validate it.
I'll keep on saying it so long as you promote an answer with a limited number of supported digits :-) Unless you highlight that as a limitation of the answer (which I don't think is unreasonable - practicalities should be observed).
Any of these will throw a Number Format Exception if you try to pass it a non-number (integral or decimal, based on the one you choose, of course).
Alternately, depending on your requirements, just iterate the characters in the String and check Digit() and/or Letter().
Number Format object to see if, after parsing the string the parser position is at the end of the string.
If it is, we can assume the entire string is numeric: Does \d in Java Regex match only latin digits? NET regexes, you'll run into a problem with other (e.g.
However, if you're calling this function a lot, and you expect many of the checks to fail due to not being a number then performance of this mechanism will not be great, since you're relying upon exceptions being thrown for each failure, which is a fairly expensive operation.
An alternative approach may be to use a regular expression to check for validity of being a number: Be careful with the above Reg Ex mechanism, though, as it'll fail if you're using non-arabic digits (i.e. This is because the "\d" part of the Reg Ex will only match [0-9] and effectively isn't internationally numerically aware. ) Or even another alternative is to use Java's built-in
I do in fact think what you're proposing is a useful answer, and had considered it myself Using regular expressions is costly in terms of performance.