Java String with only digits

Introduction

Checks if the String contains only unicode digits.

A decimal point is not a unicode digit and returns false.

isNumeric(null)   = false
isNumeric("")     = true
isNumeric("  ")   = false
isNumeric("123")  = true
isNumeric("12 3") = false
isNumeric("ab2c") = false
isNumeric("12-3") = false
isNumeric("12.3") = false
public class Main {
    public static void main(String[] argv) throws Exception {
        String str = "234";
        System.out.println(isNumeric(str));
    }/*  w  w w  .j  a  v a  2  s.  c  o  m*/
    public static boolean isNumeric(String str) {
        if (str == null) {
            return false;
        }
        int sz = str.length();
        for (int i = 0; i < sz; i++) {
            if (Character.isDigit(str.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }


}



PreviousNext

Related