简体   繁体   English

检查输入的字符串是否为整数

[英]To check if an entered string is integer

In this program I want to check if entered number is 10 digit integer mobile number its working fine if i enter value as 0123456789 but it fails if i enter like 5028608722.Program is correct but something is missing or wrong here. 在此程序中,我想检查输入的数字是否为10位整数移动电话,如果我输入的值是0123456789,则可以正常工作,但是如果我输入的则是5028608722,它将失败。

package games; 包装游戏;

import java.util.Scanner;

class Utility
{
    static boolean numberOrNot(String input)
    {
        try
        {
            int  i=Integer.parseInt(input);
            System.out.println(i);
        }
        catch(NumberFormatException ex)
        {
            return false;
        }
        return true;
    }
}

public class CheckMobileNumber
{
    public static void main(String[] args)
    {
        System.out.println("Enter your mobile number");

        Scanner sc = new Scanner(System.in);

        String input = sc.next();

        if(Utility.numberOrNot(input) && (input.length() == 10))
        {
            System.out.println("Good!!! You have entered valid mobile number");
        }
        else
        {
            System.out.println("Sorry!!!! You have entered invalid mobile number. Try again...");
        }
    }
}

Try using the long (long integer) data type instead of int - I think you're running out of space: 2 ^ 32 = 4,294,967,296. 尝试使用long (长整数)数据类型而不是int我认为您空间不足:2 ^ 32 = 4,294,967,296。

Here's a quick look at the primitive data types . 快速浏览一下原始数据类型

The direct cause of your problem is that a 32-bit int type is not enough for holding all 10 digit integers. 问题的直接原因是32位int类型不足以容纳所有10位整数。

You'd better use a regular expression for this task. 您最好为此使用正则表达式。 Technically, phone numbers are not integers, they just look like integers in this case. 从技术上讲,电话号码不是整数,在这种情况下,它们就像整数一样。 You are better off not to store them as integers in your software. 最好不要在软件中将它们存储为整数。 Someday you may need to deal with non-digit characters in the phone numbers. 有一天,您可能需要处理电话号码中的非数字字符。

    Pattern phoneNumberPattern = Pattern.compile("\\d{10}");

    if (phoneNumberPattern.matcher("5028608922").matches()) {
        System.out.println("OK, phone number!");
    } else {
        System.out.println("Bad phone number!");
    }

Your big value (5028608722) cannot fit into a signed int. 您的大价值(5028608722)无法放入带符号的int中。 The max signed int is 2^31 - 1 == 2147483647. The number you are working with is 5028608722, which is too large. 最大有符号整数为2 ^ 31-1 ==2147483647。正在使用的数字是5028608722,该数字太大。 In order for your code to work, you need to change to using long types. 为了使代码正常工作,您需要更改为使用长类型。 For instance: 例如:

import java.util.Scanner;

class Utility
{
    static boolean numberOrNot(String input)
    {
        try
        {
            long  i=Long.parseLong(input);
            System.out.println(i);
        }
        catch(NumberFormatException ex)
        {
            return false;
        }
        return true;
    }
}

public class CheckMobileNumber
{
    public static void main(String[] args)
    {
        System.out.println("Enter your mobile number");

        Scanner sc = new Scanner(System.in);

        String input = sc.next();

        if(Utility.numberOrNot(input) && (input.length() == 10))
        {
            System.out.println("Good!!! You have entered valid mobile number");
        }
        else
        {
            System.out.println("Sorry!!!! You have entered invalid mobile number. Try again...");
        }
    }
}

An optional solution to using Long.parseLong() is to use regular expressions to match your number. 使用Long.parseLong()一种可选解决方案是使用正则表达式来匹配您的数字。 For instance, a regex like this will return true for 10 digit numbers, otherwise false: 例如,这样的正则表达式将为10位数字返回true,否则返回false:

"0123456789". match("[0-9]{10}")

will return true. 将返回true。 Any non-numeric value, or any numeric value that is NOT 10 digits will return false. 任何非数字值或任何非10位数字的值都将返回false。

So your code could look like: 因此您的代码可能如下所示:

import java.util.Scanner;

class Utility
{
  static boolean numberOrNot(String input)
  {
      return input.matches("[0-9]{10}");
  }
}

public class CheckMobileNumber
{
    public static void main(String[] args)
    {
        System.out.println("Enter your mobile number");

        Scanner sc = new Scanner(System.in);

        String input = sc.next();

        if(Utility.numberOrNot(input))
        {
            System.out.println("Good!!! You have entered valid mobile number");
        }
        else
        {
            System.out.println("Sorry!!!! You have entered invalid mobile number. Try again...");
        }
    }
}

In addition to the correct, plain java answers: I'd suggest to use lib phonenumber for phone numbers. 除了正确的普通Java答案之外:我建议使用lib phonenumber作为电话号码。 github.com/googlei18n/libphonenumber if your interest is not purely academic ;-) github.com/googlei18n/libphonenumber(如果您的兴趣并非纯粹是学术性的;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM