如何解决java.util.InputMismatchException

时间:2013-11-29 08:34:30

标签: java

我正在研究java实验室,第一步是从输入文本文件中读取数据。我一直在尝试修复代码,但它根本没用。你们可以看看,让我知道我能做些什么吗? 我得到的错误是:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at Restaurant.<init>(Restaurant.java:35)
at RestaurantTester.main(RestaurantTester.java:11)

对于带有main方法的测试器类

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class RestaurantTester {
   private static Scanner buffer = new Scanner(System.in);
   private static int inputInt; 
   private static Restaurant restaurant;


    public static void main(String[] args) throws FileNotFoundException {
      restaurant = new Restaurant();
      System.out.print("\n                 Welcome to Java Restaurant\n");
      System.out.print("\n\n*************************************\n");
      System.out.print("1. Display Menu\n");
      System.out.print("2. Display Server List\n");
      System.out.print("3. Restaurant Activities\n");
      System.out.print("4. Quit\n");
      System.out.print("*************************************\n");
      System.out.print("Enter choice: ");
      inputInt = buffer.nextInt();
      while (inputInt != 4) {
         switch (inputInt) {
            case 1: {
               restaurant.displayMenu();
               break;
            } // end case 1
            case 2: {
               restaurant.displayServerList();
               break;
            } //end case 2
            case 3:{
               System.out.print("\n\n*************************************\n");
               System.out.print("1. Restaurant Activity\n");               
               System.out.print("2. Quit\n");
               System.out.print("*************************************\n");
               System.out.print("Enter choice: ");
               inputInt = buffer.nextInt();
               while (inputInt != 2) {
                  restaurant.restaurantActivity();
                  System.out.print("\n\n*************************************\n");
                  System.out.print("1. Restaurant Activity\n");               
                  System.out.print("2. Quit\n");
                  System.out.print("*************************************\n");
                  System.out.print("Enter choice: ");
                  inputInt = buffer.nextInt();                  
               } // end inner while  
               break;
            } // end case 3 
        } // end switch
        System.out.print("\n\n*************************************\n");
        System.out.print("1. Display Menu\n");
        System.out.print("2. Display Server List\n");
        System.out.print("3. Restaurant Activities\n");
        System.out.print("4. Quit\n");
        System.out.print("*************************************\n");
        System.out.print("Enter choice: ");
        inputInt = buffer.nextInt();
      } // end outer while

      System.out.print("\nThank you. The Java restaurant is now closed.\n");
   }  // end main
    }

我的餐厅课程

import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class Restaurant {
...             
  private Menu menu;
  public ArrayList<Server> servers; 
  private Activity activity;
  public Restaurant() throws FileNotFoundException {
     input = new Scanner(new File("menu.txt"));      
     menu = new Menu();
     servers = new ArrayList<Server>();

     temp = input.nextLine();  // skip 1st line             
     for (int index = 0; index < 3; index++) {
        servers.add(new Server(input.next(), (input.nextLine()).split(",",6)));             
     } // assume only 6 tables for each server  

     temp = input.nextLine(); // skip instruction line
     while (input.hasNext()) {
        str1 = input.next();    
        str2 = input.next(); 
        value = input.nextDouble();                 
        menu.setMenuItem(str1,str2, value);
     }                 
   } // end constructor 
    ....
}

继承我的文本文件:

Waiters: first name followed by table list
John 1,2,5,9,11,15
Maria 3,4,6,7,17,18
Mike 8,10,12,13,14,26

Menu: listing of the full menu: item code, name, price
A1 Bruschetta 5.29
A2 Caprese_Flatbread 6.10
A3 Artichoke-Spinach_Dip 3.99
A4 Lasagna_Fritta 4.99
A5 Mozzarella_Fonduta 5.99
E1 Lasagna_Classico 6.99
E2 Capellini_Pomodoro 7.99
E3 Eggplant_Parmigiana 8.99
E4 Fettuccine_Alfredo 7.49
E5 Tour_of_Italy 14.99
D1 Tiramisu 2.99
D2 Zeppoli 2.49
D3 Dolcini 3.49
S1 Soda 1.99
S2 Bella_Limonata 0.99
S3 Berry_Acqua_Fresca 2.88

2 个答案:

答案 0 :(得分:0)

您需要再跳过一行。

Skip one
read three
skip blank line?
skip instructions
loop till the end

来自http://www.java-made-easy.com/java-scanner-help.html

问:如果我使用Java的扫描仪扫描一个空白行会怎么样?

答:这取决于。如果您使用的是nextLine(),则会将空白行作为空字符串读入。这意味着如果要将空行存储在String变量中,则变量将保留为“”。它不会存储“”或者放置了许多空格。如果你正在使用next(),那么它根本不会读取空白行。它们完全被跳过了。

我的猜测是nextLine()仍然会在一个空白行上触发,因为从技术上来说,Scanner将具有空字符串“”。所以,你可以检查s.nextLine()。equals(“”)

答案 1 :(得分:0)

我可能错了,但在完成文件的第一部分后,似乎需要跳过两行。你跳过一行,但这可能只是行空间。所以你需要再次跳过才能找到你想要的内容。尝试添加其他nextLine()

input.nextline();
temp = input.nextLine();

另外,在nextDouble()之后,扫描仪可能无法转到下一行。如果以上操作不起作用,请尝试在其后添加nextLine()并查看是否有效?

value = input.nextDouble();
input.nextLine();

考虑使用split来避免此问题

while (input.hasNextLine()) {
    String line = input.nextLine();
    String[] token = line.split("\\s+");

    str1 = tokens[0].trim();    
    str2 = tokens[1].trim();
    value = Double.parseDouble(tokens[2].trim());                 
    menu.setMenuItem(str1,str2, value);
 } 

上面的代码将逐行读取每一行,然后将三行拆分为String s的数组。最后一个值需要解析为double


编辑:这是另一个

servers.add(new Server(input.next(), (input.nextLine()).split(",",6)));

你正在读下一个,然后是nextLine。你需要在一行中阅读所有内容


编辑:尝试此代码

public Restaurant() throws FileNotFoundException {
     input = new Scanner(new File("menu.txt"));      
     menu = new Menu();
     servers = new ArrayList<Server>();

     temp = input.nextLine();  // skip 1st line             
     for (int index = 0; index < 3; index++) {
         String line = input.nextLine();
         String[] tokens = line.split("[\\s,]+");
         String name = tokens[0];

         String[] nums = new String[6];
         for (int i = 1; i < tokens.length; i++) {
             nums[i - 1] = tokens[i].trim();
         }
         servers.add(new Server(name, nums));             
     } // assume only 6 tables for each server  

     input.nextLine();
     temp = input.nextLine(); // skip instruction line

     while (input.hasNextLine()) {
         String line = input.nextLine();
         String[] tokens = line.split("\\s+");

         str1 = tokens[0].trim();    
         str2 = tokens[1].trim();
         value = Double.parseDouble(tokens[2].trim());                 
         menu.setMenuItem(str1,str2, value);
     }                 
}