【问题标题】:Scanner class and an Exception in thread "main" java.util.InputMismatchException error扫描程序类和线程“main”中的异常 java.util.InputMismatchException 错误
【发布时间】:2013-12-09 03:12:40
【问题描述】:

我一直在努力学习扫描仪课程。我似乎无法理解它的方法。我正在尝试运行一组对我来说似乎正确的代码。我尝试了一些调整,但仍然没有。为什么我收到此错误的任何提示

“线程“主”中的异常 java.util.InputMismatchException 在 java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(未知来源)在 java.util.Scanner.nextInt(未知来源)在 java.util.Scanner.nextInt(未知来源)在 PolynomialTest.main(PolynomialTest.java:18)"

public class PolynomialTest {

public static void main(String[] args) throws IOException {

Scanner fileData = new Scanner(new File("operations.txt"));
Polynomial math = new Polynomial();
int coeff = fileData.nextInt();
int expo = fileData.nextInt();

while (fileData.hasNext()) {

    Scanner nextTerm = new Scanner(fileData.nextLine());

    if (nextTerm.next().equalsIgnoreCase("insert")) {

        math.insert(coeff, expo);
        System.out.println("The term (" + coeff + ", " + expo
                + ") has been added to the list in its proper place");
        System.out.println("The list is now: " + math.toString());

    } else if (nextTerm.next().equalsIgnoreCase("delete")) {

        math.delete(coeff, expo);
        System.out.println("The following term has been removed ("
                + coeff + ", " + expo + ")");
        System.out.println("The list is now: " + math.toString());

    } else if (nextTerm.next().equalsIgnoreCase("reverse")) {

        math.reverse();
        System.out
                .println("The list was reversed in order and the list is now: "
                        + math.toString());

    } else if (nextTerm.next().equalsIgnoreCase("product")) {

        System.out.println("The product of the polynomial is: "
                + math.product());

    } else {

        System.out.println("Not a recognized input method");

    }
    nextTerm.close();
}
PrintWriter save = new PrintWriter("operations.txt");
save.close();
fileData.close();

}

【问题讨论】:

    标签: java java.util.scanner


    【解决方案1】:

    您的代码存在许多问题。在while(hasNext()) 之后从未调用过nextLine()。你的while 循环应该是

    while (fileData.hasNextLine()) {
       Scanner nextTerm = new Scanner(fileData.nextLine());
    

    您在每个if-else 语句中都调用了nextTerm.next()。您应该分配一个名为 operation 的 String 变量。

       String operation=nextTerm.next();
    
       if (operation.equalsIgnoreCase("insert")) {
          ....
       } else if (operation.equalsIgnoreCase("delete")) {
        ..........
       } 
       ...
       else if (operation.equalsIgnoreCase("product")) {
         .....
       } 
    

    【讨论】:

    • hasNextLine() 是正确的扫描方法吗?我还没有尝试过代码,但我只是在问,因为我们被教导 hasNext() 如果有另一个非空白字符串则返回 true,如果没有则返回 false。它们是一样的吗?
    • @Brwski,hasNext() 逐个空间遍历,而 hasNextLine() 逐行遍历。由于您在 while 循环后使用了 nextLine(),所以您应该使用 hasNextLine()。
    • 它没有用。也许这与我的 coeff 和 expo 变量有关。当我尝试使用这些变量进行回声打印时,这会是一个问题吗?是否有另一种方法可以在不使用这些变量的情况下回显打印添加或删除的内容?
    【解决方案2】:

    InputMismatchException 由 Scanner 引发,表示检索到的令牌与预期类型的​​模式不匹配,或者令牌超出预期类型的​​范围。

    int coeff = fileData.nextInt();
    int expo = fileData.nextInt();
    

    尝试将上面更改为如下。(如果您在 2 个单独的行中有前 2 个整数输入,否则在使用 fileData.nextLine().split(" ") 读取它们后尝试解析它们)

    int coeff  = Integer.parseInt(fileData.nextLine());
    int expo = Integer.parseInt(fileData.nextLine());
    

    如果同一行中有 2 个整数

        String s[] = fileData.nextLine().split(" ");
        int coeff   = Integer.parseInt(s[0]);
        int expo  = Integer.parseInt(s[1]);
    

    如果您也可以发布您的 Polynomial 课程,那就太好了...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-10
      • 2022-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多