Breaking News
Home / Java / Java Snippets / How to check if a String is a number in Java?

How to check if a String is a number in Java?

Snippet

  1. public boolean isNumeric(String s) {
  2.           boolean isNumber = false;
  3.           if (s == null) {
  4.                return isNumber;
  5.           }
  6.  
  7.           for (int i = 0; i < s.length(); i++) {
  8.                char c = s.charAt(i);
  9.  
  10.                if (!Character.isDigit(c)) {
  11.                     return isNumber;
  12.                } else
  13.                     isNumber = true;
  14.           }
  15.           return isNumber;
  16.      }
[pb_builder]

Check Also

How to progamatically get the Java version?

Snippet The key here is to use the system property java.version and get minor and …

Advertisment ad adsense adlogger