简体   繁体   中英

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.

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.

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.

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. The max signed int is 2^31 - 1 == 2147483647. The number you are working with is 5028608722, which is too large. 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. For instance, a regex like this will return true for 10 digit numbers, otherwise false:

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

will return true. Any non-numeric value, or any numeric value that is NOT 10 digits will return 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. github.com/googlei18n/libphonenumber if your interest is not purely academic ;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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