【问题标题】:InputMismatchException happens when I ran jar from cmd and doesn't happen if run from ideaInputMismatchException 在我从 cmd 运行 jar 时发生,如果从 idea 运行则不会发生
【发布时间】:2015-10-04 17:54:59
【问题描述】:

在我的主要方法中,我有以下代码片段:

            try {
                select = scanner.nextInt();
            } catch (InputMismatchException e) {
                scanner.next(); //we should read erroneous
                System.out.println("Error. Please input number.");
                continue;
            }

我让 mvn clezan install -> 进入目标目录。并开始使用应用程序 实际上我输入了数字,但在控制台中我看到以下消息:

D:\freelance\Новая папка\myrepository\target>java -jar palindrome-artifactId-1.0
-SNAPSHOT.jar
Please type your name:
u1
Please select menu item
1 - suggest word, 2 - change user, 3 - my score, 4 - my word list, 5 - records,
6 - exit
1
Error. Please input number.
Please select menu item
1 - suggest word, 2 - change user, 3 - my score, 4 - my word list, 5 - records,
6 - exit

当我从想法调用应用程序(选择主方法)时,我没有看到这个问题。

请帮助解决我的问题。

附言

完整的主要方法:

public static void main(String[] args) {
    System.out.println("Please type your name:");
    try (Scanner scanner = new Scanner(System.in)) {
        scanner.useDelimiter("\n");
        String userName = scanner.next();
        Game game = new Game(userName);
        AtomicInteger atomicInteger = new AtomicInteger();
        int select = 0;
        do {
            System.out.println("Please select menu item");
            System.out.println("1 - suggest word, 2 - change user, 3 - my score, 4 - my word list, 5 - records, 6 - exit");
            try {
                select = scanner.nextInt();
            } catch (InputMismatchException e) {
                scanner.next(); //we should read erroneous
                System.out.println("Error. Please input number.");
                continue;
            }

            switch (select) {
                case 1:
                    System.out.print("Word:");
                    String word = scanner.next();
                    if (game.suggestWord(word)) {
                        System.out.println("Accepted: your score - " + game.getCurrentUserScore());
                    } else {
                        System.out.println("Rejected: Word already exists in your list or it is not palindrome");
                        System.out.println("Your score - " + game.getCurrentUserScore());
                    }
                    break;
                case 2:
                    System.out.print("Name:");
                    String name = scanner.next();
                    game.changeUser(name);
                    System.out.println("User changed successfully. Your score - " + game.getCurrentUserScore());
                    break;
                case 3:
                    System.out.println("Your score - " + game.getCurrentUserScore());
                    break;
                case 4:
                    System.out.println("Accepted words:");
                    game.getCurrentUserAcceptedWords().forEach(System.out::println);
                    break;
                case 5:
                    atomicInteger.set(1);
                    game.getScores().forEach((k, v) -> System.out.println("#" + atomicInteger.getAndIncrement() + ". name: " + k + " , score: " + v));
                    break;
                case 6:
                    System.out.println("Goodbye! thanks for the game");
                    break;
                default:
                    System.out.println("You selected nonexistent menu item. Please try one more time.");
            }

        } while (select != 6);
    }
}

【问题讨论】:

  • 您应该将更多的来源放在问题中 - 而不是链接。特别是Scanner的定义和用法一直到你展示的sn-p。
  • @RealSkeptic 主题已更新
  • 好的。你在 Windows 上工作吗?
  • 您的userName 提供了什么输入?
  • @RealSkeptic 是窗口

标签: java windows io java.util.scanner


【解决方案1】:

您的问题可能是您的分隔符。它正在寻找\n,但在Windows 上,每一行都以\r\n 结尾。所以你得到的用户名可能是"u1\r",虽然你没有看到\r(检查它的长度),你输入的数字被读作"1\r",它不能被解析为数字。

我猜你的 IDE 中的模拟控制台会将你按下的 return 解释为 \n,这就是它在那里工作的原因。

因此,将分隔符从 \n 改为 \r?\n。分隔符是一个正则表达式,这意味着“可选回车,后跟换行”。

【讨论】:

    猜你喜欢
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-15
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多