Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 继承时出现输入不匹配异常错误_Java_Inheritance_Abstract_Comparable_Mismatch - Fatal编程技术网

Java 继承时出现输入不匹配异常错误

Java 继承时出现输入不匹配异常错误,java,inheritance,abstract,comparable,mismatch,Java,Inheritance,Abstract,Comparable,Mismatch,所以我在写一个程序,我必须有一个项目列表,要么是游戏,要么是电影。每个项目都有一个3位数的ID号(保存为字符串)、一个标题、一个价格、一个库存检查(输入/输出)和一个承租人名称(名称或空)。我必须有一个菜单项目的清单,上面有与这些项目相关的东西。item类是抽象的,电影和游戏都扩展了它。以下是电影商店代码: import java.util.*; import java.io.*; public class MovieStore { private Item[] items=null;

所以我在写一个程序,我必须有一个项目列表,要么是游戏,要么是电影。每个项目都有一个3位数的ID号(保存为字符串)、一个标题、一个价格、一个库存检查(输入/输出)和一个承租人名称(名称或空)。我必须有一个菜单项目的清单,上面有与这些项目相关的东西。item类是抽象的,电影和游戏都扩展了它。以下是电影商店代码:

import java.util.*;
import java.io.*;
public class MovieStore
{
  private Item[] items=null;
  private int numItems;

  public MovieStore() //default constructor 
  {
    items=new Item[30];
    numItems=0;
  }

  public MovieStore(String testFileName) throws IOException
  {
    File f=new File(testFileName);
    if(!f.exists())
    {
      System.out.println("File "+testFileName+" does not exist.");
      System.exit(0);
    }
    Scanner input=new Scanner(f);

    items=new Item[30];
    numItems=0;
    String code;
    while (input.hasNext())
    {
      code=input.next();
      if (code.equals("M"))
      {
        String id=input.next();
        String title=input.next();
        double price=input.nextDouble();
        int runTime=input.nextInt();
        String rating=input.next();
        String format=input.next();
        int stockcheck=input.nextInt();
        String renter=input.next();
        if (stockcheck==1) //true, in stock
          items[numItems]=new Movie(id, title, price, true, runTime, rating, format);
        else //false, not in stock
          items[numItems]=new Movie(id, title, price, false, renter, runTime, rating, format);
      }
      else if (code.equals("G"))
      {
        String id=input.next();
        String title=input.next();
        double price=input.nextDouble();
        int stockcheck=input.nextInt();
        int ageLevel=input.nextInt();
        String renter=input.next();
        if (stockcheck==1) //true, in stock
          items[numItems]=new Game(id, title, price, true, ageLevel);
        else //false, not in stock
          items[numItems]=new Game(id, title, price, false, renter, ageLevel);
      }
      numItems++;
    }
    SelectionSort.sort(items, numItems);
  }

  public int search(String id)
  {
    Item key=new Item(id, "", 0.0, true, ""); 
    return (BinarySearch.search(items, numItems, key)); 
  }

  public void addItem(Item s) 
  {
    items[numItems]=s;
    numItems++;
    SelectionSort.sort(items, numItems); 
  }


  public static String menu()
  {
    String code="";
    Scanner input=new Scanner(System.in);
    System.out.println("Enter one of the following options as a letter.");
    System.out.println("a. Check out an item.");
    System.out.println("b. Check in an item.");
    System.out.println("c. Search an item by ID to see if it is in stock.");
    System.out.println("d. Search an item by name.");
    System.out.println("e. Display inventory.");
    System.out.println("f. Add a new item to the inventory.");
    System.out.println("g. Delete an item from the inventory.");
    System.out.println("h. Display the menu.");
    System.out.println("i. Exit.");
    return (input.next());
  }

  public void performAction(String choice)
  {
    Scanner input=new Scanner(System.in);
    if (choice.equalsIgnoreCase("a"))
    {
      BinarySearch s=new BinarySearch();
      System.out.println("Enter the ID Number of the item.");
      String id=input.next();
      int search=search(id);
      if (search==-1)
        System.out.println("Item not found. Try again.");
      else
      {
        if (items[search].getStock()==false)
          System.out.println("Sorry, this item is out of stock.");
        else
        {
          System.out.println("Please enter the renter's name in the format last,first [with no spaces].");
          String name=input.next();
          items[search].setName(name);
          items[search].setStock(false);
          System.out.println("Item checked out.");
        }
      }
    }
    else if (choice.equalsIgnoreCase("b"))
    {
      BinarySearch s=new BinarySearch();
      System.out.println("Enter the ID Number of the item.");
      String id=input.next();
      int search=search(id);
      if (search==-1)
        System.out.println("Item not found. Try again.");
      else
      {
        if (items[search].getStock()==true)
          System.out.println("This item is already in stock.");
        else
        {
          items[search].setName("");
          items[search].setStock(true);
          System.out.println("Item checked in.");
        }
      }
    }
    else if (choice.equalsIgnoreCase("c"))
    {
      BinarySearch s=new BinarySearch();
      System.out.println("Enter the ID Number of the item.");
      String id=input.next();
      int search=search(id);
      if (search==-1)
        System.out.println("Item not found. Try again.");
      else
      {
        if (items[search].getStock()==true)
        {
          System.out.println("This item is in stock.");
          System.out.println(items[search]);
        }
        else
        {
          System.out.println("This item is not in stock.");
          System.out.println(items[search]);
        }
      }
    }
    else if (choice.equalsIgnoreCase("d"))
    {
      System.out.println("Enter the title of the item [using dashes instead of spaces].");  
      String title=input.next();
      for (int i=0; i<numItems; i++)
      {
        if (title.equals(items[i].title))
        {
          if (items[i].getStock()==true)
          {
            System.out.println("This item is in stock.");
            System.out.println(items[i]);
          }
          else
          {
            System.out.println("This item is not in stock.");
            System.out.println(items[i]);
          }
        }
      }
    }
    else if (choice.equalsIgnoreCase("e"))
    {
      for (int i=0; i<numItems; i++)
        items[i].display();
    }
    else if (choice.equalsIgnoreCase("f"))
    {
      System.out.println("Please enter an M for a movie or a G for a game.");
      String type=input.next();
      if (type.equalsIgnoreCase("M"))
      {
        System.out.println("Please enter the ID number for the movie.");
        String id=input.next();
        System.out.println("Please enter the movie's title (no spaces, dashes instead).");
        String title=input.next();
        System.out.println("Please enter the price of the movie.");
        double price=input.nextDouble();
        System.out.println("Please enter the run time of the movie.");
        int runTime=input.nextInt();
        System.out.println("Please enter the rating of the movie.");
        String rating=input.next();
        System.out.println("Please enter the format of the movie, V(HS) or D(VD).");
        String format=input.next();
        Item s=new Movie(id, title, price, true, runTime, rating, format);
        addItem(s);
      }
      else if (type.equalsIgnoreCase("G"))
      {
        System.out.println("Please enter the ID number for the game.");
        String id=input.next();
        System.out.println("Please enter the games's title (no spaces, dashes instead).");
        String title=input.next();
        System.out.println("Please enter the price of the game.");
        double price=input.nextDouble();
        System.out.println("Please enter the age level of the game.");
        int ageLevel=input.nextInt();
        Item s=new Game(id, title, price, true, ageLevel);
        addItem(s);
      }
      else
        System.out.println("Invalid input. Try again.");
    }
    else if (choice.equalsIgnoreCase("g"))
    {
      System.out.println("Delete item.");
    }
  }


  public static void  main(String [] args) throws IOException
  {
    MovieStore m = new MovieStore("data.txt"); 
    String choice;
    do
    {
      System.out.println();
      choice = m.menu();
      m.performAction(choice);  
      System.out.println();
    } while (!choice.equalsIgnoreCase("I")); //exits when you click i
  }
}
下面是我运行程序时遇到的错误:

java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091)
    at java.util.Scanner.nextInt(Scanner.java:2050)
    at MovieStore.<init>(MovieStore.java:51)
    at MovieStore.main(MovieStore.java:233)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
java.util.InputMismatchException
位于java.util.Scanner.throwFor(Scanner.java:840)
下一步(Scanner.java:1461)
位于java.util.Scanner.nextInt(Scanner.java:2091)
位于java.util.Scanner.nextInt(Scanner.java:2050)
在MovieStore(MovieStore.java:51)
在MovieStore.main(MovieStore.java:233)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)中
位于java.lang.reflect.Method.invoke(Method.java:597)
位于edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

如果你能解释一下错误,让我知道我做错了什么,那就太好了!谢谢大家

我本想将此作为评论添加,但我还没有声誉添加评论

看起来问题在于输入文件,而不一定是代码

如果您阅读了API中关于在使用scanner对象时可能导致InputMismatchException的内容,这将是有意义的

输入文件的第二行在电影/游戏的租用者的名称中有一个空格。scanner.next()调用将读取缓冲区中直到该空间的数据。这会导致扫描仪拾取名称,直到空间中的点,然后名称的其余部分等待读取。调用scanner.nextInt()后,您希望读取一个int,但缓冲区上有一个字符串导致InputMismatch异常

G 698 Drakes-Uncharted 3.90 0 16 White, Walter

空间好点!
G 698 Drakes-Uncharted 3.90 0 16 White, Walter