通过nextInt()获取输入时的InputMismatchError

时间:2014-02-02 20:32:00

标签: java java.util.scanner numberformatexception

public void decrypt() {
    Scanner get = new Scanner(System.in);
    System.out.println("\n1. Enter Cypher Numbers\n2. Force Decode");
    if (get.next().equals("1")) {
        System.out.println("Please enter the first cipher number: ");
        this.cipherA = get.nextInt();
        System.out.println("Please enter the secondary cipher number: ");
        this.cipherB = get.nextInt();
        int multiplier = cipherA * cipherB;
        get.nextLine();
        System.out.println("Please enter the number to be decoded: ");
        String input = get.next();


        int y = 0;
        for (int x = input.length(); x >= 0; x--) {
            while (Integer.parseInt(input.substring(x-y, x))%multiplier != 0) {
                y++;
            }
            if ((Integer.parseInt(input.substring(x-y, x))/multiplier) == 27) {
                System.out.print(" ");
            } else if ((Integer.parseInt(input.substring(x-y, x))/multiplier) == 28) {
                System.out.print(".");
            } else
                System.out.print(Character.toString((char)(Integer.parseInt(input.substring(x-y, x))/multiplier+64)));
        }
    }

}

当它到达终点并尝试将get.nextInt()用于numInput时,它会停止并抛出InputMismatchError。我已经在这里切换了代码,但是当我刚刚使用String input = get.next()时它抛出了相同的错误,并且最重要的是没有保存输入的字符串的值。它只是抛出(线程中的异常“main”java.lang.NumberFormatException:对于输入字符串:“”)。这样它实际上会保存值,但随后我再次得到不匹配错误。我不知道为什么会这样。

输入是:

第一个密码:23

第二个Cypher:43

要解码的价值:7912494511868118681483526703227471483517802118683956

预期答案:你好世界

0 个答案:

没有答案