InputMismatchException错误 [英] InputMismatchException Error

查看:103
本文介绍了InputMismatchException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个编译时错误消息:



不能抛出InputMismatchException类型的异常;异常类型必须是Throwable InputMismatchException.java的子类。

据我所知,InputMismatchException是扫描程序在收到无效输入时抛出的异常,为什么呢?

  import java.util。*? 
公共类InputMismatchException
{
public static void main(String [] args)
{
boolean continueInput = true;
扫描仪输入=新的扫描仪(System.in);
do
{
try
{
System.out.println(输入整数:);
int num = input.nextInt();
System.out.println(您输入的是: + num);
continueInput = false;
}
catch(InputMismatchException e)//这是发生错误的地方。
{
System.out.println(输入整数!);
input.nextLine();
}
} while(continueInput);
}
}


解决方案

尝试为您的班级使用其他名称。如果已经有一个名为 InputMismatchException 的类,那么这已经是异常类的名称了。


I am receiving a compile time error saying:

No exception of type InputMismatchException can be thrown; an exception type must be a subclass of Throwable InputMismatchException.java

As far as I'm aware InputMismatchException is an exception thrown by the Scanner when it receives invalid input, why then is this error preventing me from compiling?

import java.util.*;
public class InputMismatchException
{
public static void main(String[] args)
{
    boolean continueInput = true;
    Scanner input = new Scanner(System.in);
    do
    {
        try
        {
            System.out.println("Enter an integer: ");
            int num = input.nextInt();
            System.out.println("You entered: " + num);
            continueInput = false;
        }
        catch (InputMismatchException e) //This is where the error occurs.
        {
            System.out.println("Enter an integer!");
            input.nextLine();
        }
    }while(continueInput);
}
}

解决方案

Try using a different name for your class. You're confusing the compiler by having a class named InputMismatchException when that is already the name of an exception class.

这篇关于InputMismatchException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆