扫描仪输入一次读取多行代码,抛出IntputMismatchException

时间:2016-10-07 07:52:24

标签: java

好的,我有这段代码:

if (type == 1){
            System.out.println("Agency name: ");
            agencyName = in.nextLine();


            System.out.println("No. of days: ");
            days = in.nextInt();


            Patuvanje p = new PraznicnoPatuvanje (agencyName, 20, days);
            nizaPatuvanje[i] = p;
        }

扫描仪正在阅读“机构名称”'和'天'没有我首先尝试输入agencyName。我应该添加什么代码,以便编译器等到我初始化agencyName变量,然后让我分配' days'变量

这就是它在控制台中的样子:

enter image description here

2 个答案:

答案 0 :(得分:0)

在此前的代码中,您正在阅读number of vacations

执行此操作后,您按Enter这也需要消耗。

将您的代码更改为

in.nextLine (); // comse previouslt entered `Enter`
System.out.println("Agency name: ");
agencyName = in.nextLine();

答案 1 :(得分:0)

   if (type == 1){
                    System.out.println("Agency name: ");
                    agencyName = in.nextLine();
                    in.nextLine();

                    System.out.println("No. of days: ");
                    days = in.nextInt();



                    Patuvanje p = new PraznicnoPatuvanje (agencyName, 20, days);
                    nizaPatuvanje[i] = p;


  }

试试这个