Java程序: - 在java中检查其手机号码?

时间:2016-08-15 15:49:46

标签: java

面试计划

准备面试@初学者水平

我在代码

下运行时遇到错误
package InterviewPrograms;

import java.util.Scanner;

class Utility
{
    static boolean numberOrNot(String input)
    {
        try
        {
            Integer.parseInt(input);
        }
        catch(NumberFormatException ex)
        {
            return false;
        }
        return true;
    }
}
public class CheckMobileNumber 
{
    @SuppressWarnings("resource")
    public static void main (String args[])
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the value to check number or not and its indian mobile number or not");
        String value = input.next();
        if(Utility.numberOrNot(value))
        {
            if(value.length() == 10)
            {
                System.out.println("Enter number is integer and its indian mobile number");
            }
            else
            {
                System.out.println("Enter number is integer but not a indian mobile number");
            }
        }
        else
        {
            System.out.println("Enter value is not a number");
        }
    }

}

错误如下

  

线程中的异常" main" java.lang.NumberFormatException:用于输入   string:" 9867233256"在   java.lang.NumberFormatException.forInputString(未知来源)at   java.lang.Integer.parseInt(未知来源)at   java.lang.Integer.parseInt(未知来源)at   InterviewPrograms.CheckMobileNumber.main(CheckMobileNumber.java:29)

提前致谢

1 个答案:

答案 0 :(得分:1)

尝试long而不是int

 Long.parseLong(input);

int提供错误,因为其范围仅为2,147,483,647(inclusive)