Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用hasNextInt()捕获异常?我需要Int,但如果输入是字符,这是坏的_Java_Exception Handling - Fatal编程技术网

Java 如何使用hasNextInt()捕获异常?我需要Int,但如果输入是字符,这是坏的

Java 如何使用hasNextInt()捕获异常?我需要Int,但如果输入是字符,这是坏的,java,exception-handling,Java,Exception Handling,我一直试图阻止异常,但我不知道如何阻止。 我尝试了parseInt,java.util.NormalExceptionMismatch等 有人知道如何解决这个问题吗?由于复制和粘贴,格式设置有点不正确 do { System.out.print( "How many integers shall we compare? (Enter a positive integer):"); select = intFind.nextInt(); if (!intFi

我一直试图阻止异常,但我不知道如何阻止。 我尝试了
parseInt
java.util.NormalExceptionMismatch

有人知道如何解决这个问题吗?由于复制和粘贴,格式设置有点不正确

do
{
   System.out.print(
           "How many integers shall we compare? (Enter a positive integer):");
   select = intFind.nextInt();
   if (!intFind.hasNextInt()) 
       intFind.next();
       {
           // Display the following text in the event of an invalid input
           System.out.println("Invalid input!");
       }
}while(select < 0)
do
{
系统输出(
“我们要比较多少个整数?(输入一个正整数):”;
select=intFind.nextInt();
如果(!intFind.hasNextInt())
intFind.next();
{
//如果输入无效,请显示以下文本
System.out.println(“无效输入!”);
}
}while(选择<0)
我尝试过的其他方法:

 do
    {
       System.out.print(
                   "How many integers shall we compare? (Enter a positive integer):");
       select = intFind.nextInt();
       {
            try{
                   select = intFind.nextInt();
               }catch (java.util.InputMismatchException e)
            {
               // Display the following text in the event of an invalid input
               System.out.println("Invalid input!");
               return;
            }
       }
    }while(select < 0)
do
{
系统输出(
“我们要比较多少个整数?(输入一个正整数):”;
select=intFind.nextInt();
{
试一试{
select=intFind.nextInt();
}catch(java.util.InputMismatchException e)
{
//如果输入无效,请显示以下文本
System.out.println(“无效输入!”);
返回;
}
}
}while(选择<0)

IMO,最佳做法是使用
nextLine()
获取字符串输入,然后使用
parseInt
获取整数。如果不可解析,只需向用户投诉并请求重新输入即可


请记住,您可能需要执行第二次
nextLine()
(放弃输入)来清除缓冲区。

IMO,最佳做法是使用
nextLine()
获取字符串输入,然后使用
parseInt
获取整数。如果不可解析,只需向用户投诉并请求重新输入即可


请记住,您可能需要执行第二次
nextLine()
(放弃输入)来清除缓冲区。

如何捕获异常的快速示例:

int exceptionSample()
{
    int num = 0;
    boolean done = false;
    while(!done)
    {
        // prompt for input
        // inputStr = read input
        try {
            num = Integer.parseInt(inputStr);
            done = true;
        }
        catch(NumberFormatException ex) {
            // Error msg
        }
    }
    return num;
}

如何捕获异常的快速示例:

int exceptionSample()
{
    int num = 0;
    boolean done = false;
    while(!done)
    {
        // prompt for input
        // inputStr = read input
        try {
            num = Integer.parseInt(inputStr);
            done = true;
        }
        catch(NumberFormatException ex) {
            // Error msg
        }
    }
    return num;
}

在我看来,在得到一个整数之前,你似乎想跳过所有内容。这里的代码跳过除整数之外的任何输入

只要没有可用的整数(而(!in.hasNextInt())),就放弃可用的输入(in.next)。当整数可用时-读取它(int num=in.nextInt();)


在我看来,在得到一个整数之前,你似乎想跳过所有内容。这里的代码跳过除整数之外的任何输入

只要没有可用的整数(而(!in.hasNextInt())),就放弃可用的输入(in.next)。当整数可用时-读取它(int num=in.nextInt();)


我不明白这个问题-您的代码没有try/catch…我尝试了try/catch,但无法使其工作。所以我改成了HasNextInt你想实现什么?你认为这些代码示例会做什么而它们不会呢?我不理解这个问题-你的代码没有try/catch…我尝试了try/catch,但无法使它工作。所以我改成了HasNextInt你想实现什么?您认为这些代码示例会做什么,而它们不会?