InputMismatchException:无法识别字符串

时间:2015-08-02 19:27:50

标签: java if-statement inputmismatchexception

我在下面写了一段Java代码。当运行它并键入任何值(任何一个定义,例如latte,或任何其他,例如az整数)时,我得到一个InputMismatchException。

就我找到答案而言,此异常意味着输入类型与预期类型不匹配。我错过了什么,为什么代码不能识别String输入?谢谢你的支持。

干杯,Gabor

package Lesson1;

import java.util.Scanner;

public class Coffee {

    public static void main(String[] args) {

        //I define the type of coffees as Strings, plus the order as String as well
        String espresso = "espresso";
        String americano = "americano";
        String cappuccino = "cappuccino";
        String latte = "latte";
        String order = new String();

        //I ask the user for their input
        Scanner choice = new Scanner(System.in);
        System.out.println("What kind of coffee would you like? We have: espresso, americano, cappuccino and latte");

        //depending on the user's choice, the corresponding name is displayed; if any other string is entered, the else clause is displayed
        if (order.equals(choice.next(espresso))) {
            System.out.println("Your order: " + espresso);

        } else if (order.equals(choice.next(americano))) {
            System.out.println("Your order: " + americano);

        } else if (order.equals(choice.next(cappuccino))) {
            System.out.println("Your order: " + cappuccino);

        } else if (order.equals(choice.next(latte))) {
            System.out.println("Your order: " + latte);

        } else {
            System.out.println("Unfortunately we can't serve you. Have a nice day!");
        }

    }

}



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.next(Unknown Source)
    at Lesson1.Coffee.main(Coffee.java:22)

2 个答案:

答案 0 :(得分:1)

您在默认输入中编写一次,但是您尝试使用choice.next(..)多次读取。

一种解决方案是在if-else语句之前在String中分配您的选择,然后使用equalsIgnoreCase进行检查。

    //I ask the user for their input
    Scanner choice = new Scanner(System.in);
    System.out.println("What kind of coffee would you like? We have: espresso, americano, cappuccino and latte");

    String picked = choice.next();
    //depending on the user's choice, the corresponding name is displayed; if any other string is entered, the else clause is displayed
    if (picked.equalsIgnoreCase(espresso)) {
        System.out.println("Your order: " + espresso);

    } else if (picked.equalsIgnoreCase(americano)) {
        System.out.println("Your order: " + americano);

    } else if (picked.equalsIgnoreCase(cappuccino)) {
        System.out.println("Your order: " + cappuccino);

    } else if (picked.equalsIgnoreCase(latte)) {
        System.out.println("Your order: " + latte);

    } else {
        System.out.println("Unfortunately we can't serve you. Have a nice day!");
    }

答案 1 :(得分:0)

我认为您使用的是扫描仪错误。尝试使用没有参数的next()方法来获取用户输入,并且只调用一次(而不是在每个if else分支内)。像这样:

package com.company;

import java.util.Scanner;

public class Coffee {
    public static void main(String[] args) {

        //I define the type of coffees as Strings, plus the order as String as well
        String espresso = "espresso";
        String americano = "americano";
        String cappuccino = "cappuccino";
        String latte = "latte";

        //I ask the user for their input
        Scanner choice = new Scanner(System.in);
        System.out.println("What kind of coffee would you like? We have: espresso, americano, cappuccino and latte");

        //depending on the user's choice, the corresponding name is displayed; if any other string is entered, the else clause is displayed

        String order = choice.next();
        if (order.equals(espresso)) {
            System.out.println("Your order: " + espresso);

        } else if (order.equals(americano)) {
            System.out.println("Your order: " + americano);

        } else if (order.equals(cappuccino)) {
            System.out.println("Your order: " + cappuccino);

        } else if (order.equals(latte)) {
            System.out.println("Your order: " + latte);

        } else {
            System.out.println("Unfortunately we can't serve you. Have a nice day!");
        }

    }
}
相关问题