Java 是否阻止用户输入大于int max的值?

Java 是否阻止用户输入大于int max的值?,java,exception,int,max,Java,Exception,Int,Max,我有一些很酷的代码,它接受int值。我让我弟弟测试,他做的第一件事是什么?他输入以下内容: 12345678910 他犯了这个错误: User did not input a number. (Error Code 1) 那不是真的。有没有办法给他一个不同的错误“值太大”?这是我的密码: try { number = input.nextInt(); } catch (InputMismatchException e) { System.err.println("User d

我有一些很酷的代码,它接受int值。我让我弟弟测试,他做的第一件事是什么?他输入以下内容:

12345678910
他犯了这个错误:

User did not input a number. (Error Code 1)
那不是真的。有没有办法给他一个不同的错误“值太大”?这是我的密码:

try
{
    number = input.nextInt();
}
catch (InputMismatchException e)
{
    System.err.println("User did not input a number. (Error Code 1)");
    System.exit(1);
}
谢谢

编辑 我使用的代码已被修改。这是我最后使用的代码,但解决方案不再出现在注释中

        try 
        {
            double intitalinput = input.nextDouble();

            if (intitalinput > Integer.MAX_VALUE)
            {
                System.err.println("User entered a number larger than " + Integer.MAX_VALUE + ". (Error Code 2)");
                System.exit(2);
            }
            else 
            {
                number = (int) intitalinput;
            }
        }
        catch (InputMismatchException e) 
        {
            System.err.println("User did not input a number. (Error Code 1)");
            System.exit(1);
        }
谢谢你解决我的问题

编辑第二个 我加了一张“少于零”的支票。如果其他人偶然发现这个问题需要类似的帮助,我将在此处显示更新的代码:

try 
{
    double intitalinput = input.nextDouble();

    if (intitalinput > Integer.MAX_VALUE)
    {
        System.err.println("User entered a number larger than " + Integer.MAX_VALUE + ". (Error Code 2)");
        System.exit(2);
    }
    else if (intitalinput < 0)
    {
        System.err.println("User entered a number smaller than 0. (Error Code 3)");
        System.exit(3);
    }
    else 
    {
        number = (int) intitalinput;
    }
}
catch (InputMismatchException e) 
{
    System.err.println("User did not input a number. (Error Code 1)");
    System.exit(1);
}
试试看
{
double intitalinput=input.nextDouble();
if(初始输入>整数最大值)
{
System.err.println(“用户输入的数字大于”+Integer.MAX_VALUE+”(错误代码2)”;
系统出口(2);
}
否则如果(初始输入<0)
{
System.err.println(“用户输入的数字小于0。(错误代码3)”;
系统出口(3);
}
其他的
{
数字=(int)初始输入;
}
}
捕获(输入不匹配异常e)
{
System.err.println(“用户未输入数字。(错误代码1)”;
系统出口(1);
}

尝试使用Scanner.hasNextInt()方法:

如果可以使用nextInt()方法将此扫描仪输入中的下一个标记解释为默认基数中的int值,则返回true。扫描仪不会超过任何输入

如果要检测输入的数字,但该数字可能太大而无法解析,请尝试以下方法:

   public static void main(String[] args) {

       Scanner input = new Scanner(System.in);
       System.out.println("Enter a number:");
       if (input.hasNextInt()) {
           int num = input.nextInt();
           System.out.println("num is " + num);
       } else {
           String s = input.next();
           boolean isaNumber = true;
           for (int i=0; i<s.length(); i++) {
               if (!Character.isDigit(s.charAt(i))) {
                   isaNumber = false;
                   break;
               }
           }
           if (isaNumber) {
               System.out.println("It appears you entered a number, but it canntot "
                    + "be read.  It might be too large.");
           } else {
               System.out.println("Error parsing number.");
           }
       }
   }
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入一个数字:”);
if(input.hasNextInt()){
int num=input.nextInt();
System.out.println(“num是”+num);
}否则{
字符串s=input.next();
布尔值=真;

对于(int i=0;i如中所示,您可以将输入存储在
long
中,然后检查数字是否太大。或者,您可以将所有值更改为long类型。

一个大的数字本身不会从编译器返回异常;您遇到的错误可能是因为
int
无法保存超过的值g大约20亿。相反,您可以尝试将
number
声明为
long
类型


另一种解决方案是首先用字符串抓取输入,如@karfau所述
方法;如果字符串超过一定数量的字符,您将知道输入太长。

您可以尝试获取一个
长的
,但这只会提高限制,而不会解决问题


另一种方法是将值作为字符串获取,并在尝试转换之前使用正则表达式检查其数值。如果转换失败,则数字太大。

有很多方法可以实现这一点,例如检查较大的数字,并根据
整数的最大和最小大小验证它ng
Integer.MAX\u值
Integer.MIN\u值

// long userInput = input.nextLong()
// or
double userInput = input.nextDouble();

// expecting an integer but user put a value larger than integer
if (userInput > Integer.MAX_VALUE || userInput < Integer.MIN_VALUE) {
   // Throw your error
}  else {
   // continue your code the number is an int
   number = (int) userInput;
}
//long userInput=input.nextLong()
//或
double userInput=input.nextDouble();
//应为整数,但用户输入的值大于整数
if(userInput>Integer.MAX|u值| userInput
您可以用try-catch块将其包围


更多详细信息,请参见的第一个答案。

这如何允许我对“数字太大”与“数字不是数字”分别做出错误?嗯……我以前使用过其他方法,但现在该解决方案变为此。奇怪。是的,没问题,我之前输入的类型错误,我不确定你是否有d注意到了。很抱歉。类型不匹配,但这是较早的一个。我只是想知道,当您将其转换为int时,会输入舍入吗?@JayHarris-请参阅我对问题的编辑。非常感谢!
// long userInput = input.nextLong()
// or
double userInput = input.nextDouble();

// expecting an integer but user put a value larger than integer
if (userInput > Integer.MAX_VALUE || userInput < Integer.MIN_VALUE) {
   // Throw your error
}  else {
   // continue your code the number is an int
   number = (int) userInput;
}