java - 解决仅出现在一个编译器中的不匹配异常

标签 java while-loop java.util.scanner mismatch inputmismatchexception

这是一项在线作业。目标是多次接受两个输入(在此代码中为 x 和 y 变量),然后打印每个变量的最大和最小值。这是代码:

import java.io.*;
import static java.lang.System.*;

import java.util.Scanner;
import java.lang.Math;


class Lesson_20_Activity_One{

     public static void main (String str[]) throws IOException {
          Scanner scan = new Scanner(System.in);
          double x; //the latitude input
          double y; //the longitude input
          double n = -90; //the maximum north value
          double e = -180; //the maximum east value
          double s = 90; //the maximum south value
          double w = 180; //the maximum west value
          int escape = 1; //the value that dictates when it's time to exit the while loop
          while (escape == 1) {
          System.out.println("Please enter the latitude:");
          x = scan.nextDouble();
          System.out.println("Please enter the longitude:");
          y = scan.nextDouble();
          if ((x>n) && (x<=90))
              n = x;
          if ((x<s) && (x>=-90))
              s = x;
          if ((y>e) && (y<=180))
              e = y;
          if ((y<w) && (y>=-180))
              w = y;
          if ((x>90)||(x<-90)||(y>180)||(y<-180))
              System.out.println("Incorrect Latitude or Longitude");
          System.out.println("Would you like to enter another location?");
          escape = scan.nextInt();
          }
          System.out.println("Farthest North: " + n);
          System.out.println("Farthest South: " + s);
          System.out.println("Farthest East: " + e);
          System.out.println("Farthest West: " + w);
     }

}

这段代码在我的个人编译器 Eclipse 上完全按照预期运行。但是,当在类(class)的在线编译器上运行相同的程序时,我遇到了以下错误:

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:322)
    at Ideone.assertRegex(Main.java:85)
    at Ideone.assertRegex(Main.java:76)
    at Ideone.test(Main.java:40)
    at Ideone.main(Main.java:29)

我该如何解决这些错误?看来是我的扫描仪有问题,但我不知道如何修复它。

最佳答案

错误跟踪似乎表明在调用 scan.nextInt() 时抛出 InputMismatchException

在打印System.out.println("Would you like to Enter another location?");之后,如果用户输入除整数以外的任何内容,scan.nextInt() 将抛出异常。即使您按照其他人的建议将用户的输入读取为字符串,escape = Integer.parseInt(scan.nextLine()) 中的 parseInt 方法也会抛出错误,因为该字符串可能不是有效整数。

我建议在 scan.nextInt() 调用周围添加 try-catch block ,这样当用户输入有效整数以外的内容(例如数字 8.238)时,程序就不会崩溃)。

boolean inputValid = false;
System.out.println("Would you like to enter another location?");
while (!inputValid) {
    try {
        escape = scan.nextInt();
        inputValid = true;
    } catch (InputMismatchException e) {
        inputValid = false;
        System.out.println("Please enter a valid Integer");
    }
}

此代码将继续询问用户,直到用户输入有效的整数。

关于java - 解决仅出现在一个编译器中的不匹配异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40025192/

相关文章:

java - 对于大于 150kb 的文件上传请求参数为 null,多部分文件上传,Spring 3.2,wildfly 9.0.0

java - Java创建目录的问题

java - 按频率排序单词? (从最低到最高)

c++ - 为什么这个循环只运行一次?

c - 查找某个范围内的所有良序数字,如何在 C 中 while 循环停止之前打印最后一个值并每行仅打印 10 个值?

Java FutureTask 异常

c++ - 可能有多个 while (cin>>input)

java - 为什么文本文件没有完全读取?

java - 使用 Java Scanner 顺序读取 stdin 时抛出异常

java - NoSuchElementException:未找到行 - Scanner Java