java 使用正则判断是不是一个数字

 

public class Numeric {

    public static void main(String[] args) {

        String string = "-1234.15";
        boolean numeric = true;

        numeric = string.matches("-?\\d+(\\.\\d+)?");

        if(numeric)
            System.out.println(string + " is a number");
        else
            System.out.println(string + " is not a number");
    }
}

 

posted @ 2018-10-24 12:58  anobscureretreat  阅读(270)  评论(0编辑  收藏  举报