📌  相关文章
📜  检查给定字符串是否是Java中的有效数字(整数或浮点数)

📅  最后修改于: 2022-05-13 01:54:58.024000             🧑  作者: Mango

检查给定字符串是否是Java中的有效数字(整数或浮点数)

在检查给定字符串是否为有效数字一文中,我们讨论了检查字符串是否为有效数字的一般方法。在Java中,我们可以使用 Wrapper 类的parse()方法和 try-catch 块来检查数字。

对于整数

Integer 类提供了一个静态方法parseInt()如果 String 不包含可解析的int ,它将抛出 NumberFormatException 。我们将使用 catch 块捕获此异常,从而确认给定的字符串不是有效的整数。下面是演示相同的Java程序。

//Java program to check whether given string
// is a valid integer number
  
class GFG 
{
    public static void main (String[] args)
    {
        String input1 = "abc";
        String input2 = "1234";
          
        try 
        {
            // checking valid integer using parseInt() method
            Integer.parseInt(input1);
            System.out.println(input1 + " is a valid integer number");
        } 
        catch (NumberFormatException e) 
        {
            System.out.println(input1 + " is not a valid integer number");
        }
          
        try 
        {
            // checking valid integer using parseInt() method
            Integer.parseInt(input2);
            System.out.println(input2 + " is a valid integer number");
        } 
        catch (NumberFormatException e)
        {
            System.out.println(input2 + " is not a valid integer number");
        }
    }
}

输出:

abc is not a valid integer number
1234 is a valid integer number

对于浮点数

Float 类提供了一个静态方法parseFloat()如果 String 不包含可解析的float将抛出 NumberFormatException 。我们将使用 catch 块捕获此异常,从而确认给定的字符串不是有效的浮点数。如果字符串为null ,此方法将抛出 NullPointerException。下面是演示相同的Java程序。

// Java program to check whether given string
// is a valid float number.
  
class GFG 
{
    public static void main (String[] args)
    {
        String input1 = "10e5.4";
        String input2 = "2e10";
          
        try
        {
            // checking valid float using parseInt() method
            Float.parseFloat(input1);
            System.out.println(input1 + " is a valid float number");
        } 
        catch (NumberFormatException e)
        {
            System.out.println(input1 + " is not a valid float number");
        }
          
        try 
        {
            // checking valid float using parseInt() method
            Float.parseFloat(input2);
            System.out.println(input2 + " is a valid float number");
        } 
        catch (NumberFormatException e)
        {
            System.out.println(input2 + " is not a valid float number");
        }
    }
}

输出:

10e5.4 is not a valid float number
2e10 is a valid float number

对于大数字

我们可以对大数使用 BigInteger 和 BigDecimal 类构造函数。下面是演示相同的Java程序。

// Java program to check whether given string
// is a valid number.
  
import java.math.BigInteger;
import java.math.BigDecimal;
  
class GFG 
{
    public static void main (String[] args)
    {
        String input1 = "1231456416541214651356151564651954156";
        String input2 = "105612656501606510651e655.4";
        String input3 = "2e102225";
          
        try 
        {
            // checking valid integer number using BigInteger constructor
            new BigInteger(input1);
            System.out.println(input1 + " is a valid integer number");
        } 
        catch (NumberFormatException e) 
        {
            System.out.println(input1 + " is not a valid integer number");
        }
          
        try 
        {
            // checking valid float number using BigDecimal constructor
            new BigDecimal(input2);
            System.out.println(input2 + " is a valid float number");
        } 
        catch (NumberFormatException e)
        {
            System.out.println(input2 + " is not a valid float number");
        }
          
        try 
        {
            // checking valid float number using BigDecimal constructor
            new BigDecimal(input3);
            System.out.println(input3 + " is a valid float number");
        } 
        catch (NumberFormatException e)
        {
            System.out.println(input3 + " is not a valid float number");
        }
          
    }
}

输出:

1231456416541214651356151564651954156 is a valid integer number
105612656501606510651e655.4 is not a valid float number
2e102225 is a valid float number