I Spy Code - Java

How to check if string is numeric

Question:

Write a java program that tests if a string is numeric.

Solution:

Here is a java example that checks if a string contains only digits:

Source: (Example.java)

import org.apache.commons.lang.StringUtils;
 
public class Example {
 
   public static void main(String[] args) {
 
      System.out.println(StringUtils.isNumeric("12345"));
      System.out.println(StringUtils.isNumeric("Five"));
      System.out.println(StringUtils.isNumeric("Hello world"));
      System.out.println(StringUtils.isNumeric("1.234"));
 
   }
}
 

Output:

$ java Example
true
false
false
false

References:

https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#isAsciiPrintable-java.lang.CharSequence-

Questions answered by this page:

How to check if a String is numeric in Java?
Java example that checks if a string is an integer