java - 我的代码中不断出现错误 "java.util.InputMismatchException"

标签 java

我一直在为一个项目编写此代码,但不断收到错误“java.util.InputMismatchException”。我搜索并发现了类似的问题,但我不知道答案如何应用于我的代码。我知道我输入的内容是正确的,所以就可以了。另外,我一直在尝试重新格式化我的代码,但这似乎只会让情况变得更糟。如果这很明显并且我不认识它,我很抱歉,我刚刚开始编码。感谢您的耐心等待。

这是完整的错误消息:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at songBottlesOfBeer.BottlesOfBeer.main(BottlesOfBeer.java:47)

这是我的完整代码:

package songBottlesOfBeer;

import java.util.Scanner;

public class BottlesOfBeer {

        private static Scanner bottles;
            public static void number(int n) {

                if (n>1) {
                    System.out.print(n+" bottles of beer on the wall, "+
                            n+" bottles of beer, ya' take one down, "+
                            "ya' pass it around, ");
                    n=n-1 ;
                    System.out.println(n+" bottles of beer on the wall.");
                    number(n);
                }else{

                    if(n==1) {
                        System.out.print(n+" bottle of beer on the wall, "+
                                n+" bottle of beer, ya' take one down, "+
                                "ya' pass it around, ");
                                n=n-1 ;
                        System.out.println(n +" bottles of beer on the wall.");
                        number(n);
                    }else{
                        System.out.println("No more bottles of beer on the wall, " +
                                "no bottles of beer, ya' can't take one down, "
                                + "ya' can't pass it around, 'cause there are"
                                + " no more bottles of beer on the wall!");
                            }

                        }
                }

            public static void main(String[] args) {
                bottles = new Scanner(System.in);
                bottles.useDelimiter("\n");

                System.out.println("Enter the starting number of " 
                        + "bottles in the song "
                        + "'99 Bottles of Beer on the Wall':");
                number(bottles.nextInt());
                }
}

错误位于 number(bottles.nextInt());

最佳答案

删除该行

bottles.useDelimiter("\n");

首先避免使用 \n 作为行分隔符,因为它依赖于操作系统。如果在正则表达式中,请使用 System.lineSeparator()\R。其次,分隔符用于将单个输入标记(分解)为多个部分,这不是您所需要的。按 Enter 会自动提交单个输入。

示例

    Scanner bottles = new Scanner(System.in);
    System.out.println(bottles.nextInt());
    System.out.println(bottles.nextInt());
    System.out.println(bottles.nextInt());

第一次调用将阻塞执行等待输入。输入由默认分隔符 \p{javaWhitespace}+ (基本上是空格)进行标记。让我们看一下以下案例:

输入:1 2 3 回车

输出:

1
2
3

发生这种情况是因为单个 5 个字符输入被标记为 3 个段,然后由 nextInt 方法按顺序调用。

输入1 2 回车

输出:

1
2
//cursor mark

发生这种情况是因为您“饱和”了前 2 次对 nextInt 的调用,但第三次没有找到另一个整数,因此它提示用户输入(并阻止执行)。

输入:1 2 3 4 回车

输出:

1
2
3

与第一种情况一样,只有现在扫描仪才会存储输入4,并且下一次调用将使用它。

完成后请记得关闭扫描仪:

bottles.close();

关于java - 我的代码中不断出现错误 "java.util.InputMismatchException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26443329/

相关文章:

java - 如何构建博客 API 多对多关系的 Firestore JSON

java - 使用 Observable 单例类来处理网络调用不好吗?

java - 超出类文件格式限制

java - 如何找到最接近另一个数字的数字,即 2 的幂?

java - 在数组中建立 Happens-Before

java - Quarkus 应用程序在 Heroku 上的部署问题

java - 两个线程访问的同步块(synchronized block)

java - protobuf为什么要使用builder来构造数据?

java - WindowBuilder - 组件大小

java - 多个 java servlet 的 web.xml 中的通用初始化参数?