2014-02-17 4 views
0

그래서 게임이나 영화 항목 목록이 필요한 프로그램을 작성하고 있습니다. 각 항목에는 3 자리 ID 번호 (문자열로 저장 됨), 제목, 가격, 재고 확인 (입/출력) 및 임차인 이름 (이름 또는 비어 있음)이 있습니다. 나는이 아이템들과 관련된 메뉴 아이템 목록을 가져야했다. 항목 클래스는 추상 클래스이며, 동영상과 게임 모두 해당 클래스를 확장합니다. 영화 스토어 코드는 다음과 같습니다.입력 상 일치하지 않는 상속 예외 오류

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 
    } 
} 

영화 및 게임 클래스는 유사합니다. Item은 비교할 수있는 것을 구현하고 ID 번호를 기반으로 비교합니다. 그것들은 아이템을 기반으로 다른 생성자를 가지고 있지만 MovieStore 프로그램에서 올바르게 생성됩니다. 여기

M 121 The-Departed 4.90 209 R D 0 Underwood,Frank 
G 698 Drakes-Uncharted 3.90 0 16 White, Walter 
M 345 Frozen 5.50 163 G D 0 Sheeran,Ed 
M 768 School-of-Rock 1.99 155 PG V 1 empty 
G 904 Lego-Batman 6.77 0 6 Pinkman,Jesse 
M 564 The-Hobbit 6.50 255 PG13 D 0 Swift,Taylor 
G 532 Wii-Sports 4.35 0 12 Goodman,Saul 
M 196 Scarface 3.68 213 R V 1 empty 
M 333 Love-Actually 4.58 130 PG13 D 1 empty 
M 889 Shutter-Island 6.98 193 PG13 D 0 Smith,Matt 
M 508 The-Notebook 3.45 175 PG13 V 1 empty 
G 132 Fifa 8.99 0 7 Stark,Ned 
G 666 Call-of-Duty 0 16 Snow,Jon 
M 401 Titanic 5.90 240 PG13 V 1 empty 
M 837 The-Amazing-Spiderman 6.89 198 PG D 0 Pond,Amy 
M 424 The-Wolf-of-Wall-Street 9.99 216 R D 0 Lannister,Tyrion 
G 999 Battlefield 7.88 1 16 empty 
G 444 Borderlands 6.44 1 14 empty 
M 774 Mean-Girls 3.50 154 PG13 V 1 empty 
M 582 Flight 5.66 201 R D 0 Montgomery,Aria 
G 999 Madden 4.33 1 10 empty 
M 130 The-Boondock-Saints 6.77 234 R V 0 Beckham,David 
M 420 Airplane! 6.55 121 PG V 1 empty 
M 699 Inception 7.56 221 PG13 D 0 Mars,Bruno 
G 100 Civilization 8.99 1 16 empty 
M 834 The-Great-Gatsby 8.55 215 PG13 D 1 empty 
M 555 The-Grinch 2.11 121 G V 1 empty 
G 333 Pokemon 1.50 1 6 empty 
M 800 21-Jump-Street 5.10 200 PG13 D 1 empty 
M 945 Pitch-Perfect 7.89 191 PG13 D 0 Cyrus,Miley 

data.txt라는 파일이며, 여기에 내가 프로그램을 실행할 때 나는 점점 오전 오류입니다 :

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) 

방금 ​​오류를 설명하고 제가 해요 알려 수 있다면을 틀리게하고있는, 그것은 위대 할 것이다! 고마워요!

답변

2

댓글로 추가했을뿐입니다. 아직 댓글을 달지는 못했습니다.

입력 파일에 문제가있는 것 같고 코드가 아닌 것 같습니다.

스캐너 개체를 사용할 때 InputMismatchException을 일으킬 수있는 API에 대해 읽는다면 의미가 있습니다.

입력 파일의 두 번째 줄에는 영화/게임의 임차인 이름에 공백이 있습니다. scanner.next() 호출은 해당 공간까지 버퍼를 읽습니다. 이것은 스캐너가 공간의 한 지점까지 이름을 집어 들고 나머지 이름이 읽히기를 기다리고 있습니다. 일단 scanner.nextInt()가 호출되면 int를 읽으려고하지만 버퍼에 문자열이있어 InputMismatch 예외가 발생합니다.

G 698 Drakes-Uncharted 3.90 0 16 White, Walter 
+0

공간이 좋은 지점! – ATG

+0

유권자를 보내 주셔서 감사합니다. :-) – Justine

관련 문제