程序中的Java运行时错误InputMismatchException

时间:2018-08-31 03:36:39

标签: java exception runtime-error inputmismatchexception

当我运行此代码时,我会在coderunner(一个提交功课代码的应用程序)中获取此代码

    Scanner scan = new Scanner(System.in);

    double maxn = -90;
    double maxs = 90;
    double maxe = 180;
    double maxw = -180;
    double lat = 0;
    double longa = 0;
    int x = 1;

    while (x != 0) {
        System.out.println("Please enter a latitude:");
        lat = scan.nextDouble();
        if (lat >= maxn && lat <= 90)
            maxn = lat;
        if (lat <= maxs && lat >= -90)
            maxs = lat;

        System.out.println("Please enter a longitude:");
        longa = scan.nextDouble();
        if (longa <= maxe && longa >= -180)
            maxe = longa;
        if (longa >= maxw && longa <= 180)
            maxw = longa;

        System.out.println("Would you like to enter another location?");
        x = scan.nextInt();

    }
    System.out.println("Farthest North: " + maxn + "\nFarthest South: " + maxs + "\nFarthest East: " + maxe + "\nFarthest West: " + maxw);

我收到以下错误:

    Runtime Error

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Lesson_20_Activity.main(Main.java:315)
at Ideone.assertRegex(Main.java:85)
at Ideone.assertRegex(Main.java:76)
at Ideone.test(Main.java:40)
at Ideone.main(Main.java:29)

我不知道此错误如何工作,因为我是编码新手。有人可以解释这意味着什么以及如何解决吗?

编辑:我的输入是

Please enter the latitude: 41.678 Please enter the longitude: 69.938 Would you like to enter another location? 1 Please enter the latitude: 41.755 Please enter the longitude: 69.862 Would you like to enter another location? 1 Please enter the latitude: 41.829 Please enter the longitude: 69.947 Would you like to enter another location? 1 Please enter the latitude: 300 Please enter the longitude: 69.947 Incorrect Latitude or Longitude Please enter the latitude: 41.827 Please enter the longitude: 69.904 Would you like to enter another location? 0 Farthest North: 41.829 Farthest South: 41.678 Farthest East: 69.947 Farthest West: 69.862

此外,我尝试将x更改为双精度和字符串输入,但没有成功。我每个人得到的错误分别是NoSuchElementError和NoSuchLineError

3 个答案:

答案 0 :(得分:1)

在以下情况下,从Javadoc抛出InputMismatchException

  

由扫描程序抛出以表明检索到的令牌没有   匹配预期类型的​​模式,或者令牌不足   预期类型的​​范围。

在您的代码中,您正在调用scan.nextInt()scan.nextDouble()。确保仅将有效的intdouble值分别传递给这些调用中的每一个。也就是说,在扫描仪期望一个int值(scan.nextInt())时输入一个双精度值将引发上述错误。

答案 1 :(得分:0)

请注意,当您不想换行时,应该使用System.out.print然后写上'\ r \ n'。

我已经运行了您的代码并获得了正确的输出,但是您必须注意到您在命令行中输入了什么。我将读取所有字符串,然后转换为所需的原始格式,以避免出现此错误。

答案 2 :(得分:0)

输入经度和纬度后,您会询问要在哪里重试的问题。

x= nextInt()

在这里,您正在接受一个int(0以关闭while循环,否则为0)。 如果在其中键入任何字符串或双精度值,将导致Inputmismtach异常。我尝试了您的代码,如果正确地给出了这些整数和小数,如下所示,它就可以正常工作。

Please enter a latitude:
12.3
Please enter a longitude:
14.3
Would you like to enter another location?
1
Please enter a latitude:
12.3
Please enter a longitude:
14.5
Would you like to enter another location?
0
Farthest North: 12.3
Farthest South: 12.3
Farthest East: 14.3
Farthest West: 14.5