Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java方法哪个检查它是一个数字_Java_Numbers_Int_Double - Fatal编程技术网

Java方法哪个检查它是一个数字

Java方法哪个检查它是一个数字,java,numbers,int,double,Java,Numbers,Int,Double,我写了一个方法,它接受一个字符串,如果它是有效的单整数或浮点数,则返回true;如果不是,则返回false 我的代码: public static boolean isDigit(String s) { boolean b; try { Integer.parseInt(s); b = true; } catch (NumberFormatException e

我写了一个方法,它接受一个字符串,如果它是有效的单整数或浮点数,则返回true;如果不是,则返回false

我的代码:

public static boolean isDigit(String s)
    {
        boolean b;
        try
        {
            Integer.parseInt(s);
            b = true;
        }
        catch (NumberFormatException e)
        {
            b = false;
        }

        try
        {
            Double.parseDouble(s);
            b = true;
        }
        catch (NumberFormatException e)
        {
            b = false;
        }

        return b;
    }

我相信有更好的写作方法。谢谢

您不需要检查它是否为
int
,因为
int
数字也可以解析为
double
。可以简化为:

public static boolean isDigit(String s)
{
    boolean b;

    try
    {
        Double.parseDouble(s);
        b = true;
    }
    catch (NumberFormatException e)
    {
        b = false;
    }

    return b;
}

您不需要检查它是否为
int
,因为
int
数字也可以解析为
double
。可以简化为:

public static boolean isDigit(String s)
{
    boolean b;

    try
    {
        Double.parseDouble(s);
        b = true;
    }
    catch (NumberFormatException e)
    {
        b = false;
    }

    return b;
}

您也可以使用正则表达式来实现这一点

 public static boolean isDigit(String s){
    String regex = "[0-9]*\\.?[0-9]*";
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(s);
    boolean b = m.matches();
    return b;
  }

您也可以使用正则表达式来实现这一点

 public static boolean isDigit(String s){
    String regex = "[0-9]*\\.?[0-9]*";
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(s);
    boolean b = m.matches();
    return b;
  }

您可以简单地执行以下操作:

 return s.matches("[+-]?\\d+(\\.\\d+)?");

如果“.”是小数的分隔符,您只需执行以下操作:

 return s.matches("[+-]?\\d+(\\.\\d+)?");

如果“.”是小数的分隔符

最轻的解决方案是这个,同样也是为了代码可读性:

public boolean isDigit(String str) {
     try {
          Integer.parseInt(str)
          return true;
     } catch (NumberFormatException: e) { return false }
}

最轻的解决方案是此解决方案,也是为了代码可读性:

public boolean isDigit(String str) {
     try {
          Integer.parseInt(str)
          return true;
     } catch (NumberFormatException: e) { return false }
}

使用Apache Commons StringUtils.isNumeric()检查字符串是否为有效数字

示例:-

StringUtils.isNumeric(“123”)=真
StringUtils.isNumeric(null)=false StringUtils.isNumeric(“”=false StringUtils.isNumeric(“”=false

使用Apache Commons StringUtils.isNumeric()检查字符串是否为有效数字

示例:-

StringUtils.isNumeric(“123”)=真
StringUtils.isNumeric(null)=false StringUtils.isNumeric(“”=false StringUtils.isNumeric(“”=false

您只需要单个数字?可能的重复可以从删除方法的前半部分开始,由于您不需要对它计算的布尔值执行任何操作。如果您不必自己编写,我建议您使用lib:如果您使用的是番石榴,
return Doubles.tryParse!=无效
您只需要一位数?可能的重复可以从删除方法的前半部分开始,因为您不需要对它计算的布尔值执行任何操作。如果您不必自己编写,我建议使用lib:如果您使用的是番石榴,
返回Doubles.tryParse(s)!=无效可以通过直接删除
b
return
ing
true
false
进一步简化。可以通过直接删除
b
return
ing
true
false
进一步简化。负数/一元正数呢?答案编辑,现在也适用于负数,谢谢@Reimeus负数/一元正数呢?答案已编辑,现在也适用于负数,谢谢@Reimeus