Java、扫描仪、输入不匹配异常

Java、扫描仪、输入不匹配异常,java,exception,inputmismatchexception,Java,Exception,Inputmismatchexception,这部分代码应该加载restoraant的名称,然后是菜单名,然后是餐名和价格,但当我运行它时,它会接收所有的名称,最后是价格,我输入了示例7.2,得到了输入不匹配异常 String newMenu = ""; String newRestaurant = ""; String[] newMenuItem = new String[10]; double[] price = new double[10]; int x = 0; while (!(newMe

这部分代码应该加载restoraant的名称,然后是菜单名,然后是餐名和价格,但当我运行它时,它会接收所有的名称,最后是价格,我输入了示例7.2,得到了输入不匹配异常

  String newMenu = "";
  String newRestaurant = "";
  String[] newMenuItem = new String[10];
  double[] price = new double[10];
  int x = 0;  


       while (!(newMenu.equals("none"))) {
           System.out.println("What is the name of the Menu you wish to create (type 'none', if you are done):");
           newMenu = scan.next();
           if (newMenu.equals("none")) {
              System.out.println("Saving entry...");
              continue;

           } else {


              System.out.println("What is the name of the Menu item you wish to create (type 'none', if you are done):");
              newMenuItem[x] = "end";
              while (!(newMenuItem[x].equals("none"))) {
                 newMenuItem[x] = scan.next();

                 if (!(newMenuItem[x].equals("none"))) {
                    System.out.println("What is the price?");
                    price[x]= scan.nextDouble();
                    x++;


                    }    
                 }    
              }
           }

如果我理解你的程序的输入,最简单的解决办法是在第79行和第80行之间添加类似的内容

while (!scan.hasNextDouble()) {
  // The next token isn't a double.
  if (scan.hasNext()) {
    scan.next();
  } else { 
    System.err.println("Nothing to read");
    System.exit(1);
  }
}

因为,目前您的
扫描仪
没有下一个符号作为
双字符

是的,但是这次代码更干净了,我修复了很多东西,这是最后一件事,也是谜题的最后一部分,如果输入数字有效,我完成了我的任务,这是一个非常糟糕的bcs,现在是凌晨4点,我需要一些睡眠,我需要在明天之前交这个morning@user2627736在java.util的线程“main”java.util.InputMismatchException中发布错误..异常。位于java.util的Scanner.throwFor(Scanner.java:909)。位于java.util的Scanner.next(Scanner.java:1530)。Scanner.nextDouble(Scanner.java:2456)在restaurantsett2.main(restaurantsett2.java:80)顺便说一句,第80行是“price[x]=scan.nextDouble();”@user2627736这只是您的输入,可能是您添加了一个字符或空格,可能是以前扫描的结果吗?因为它们是弦。如果是剩菜,我该怎么清洗呢。我只输入7.2,当它询问价格时按回车键