书店售货亭没有此类元素例外

时间:2013-12-03 00:55:45

标签: java kiosk

所以这是一项艰巨的工作。我正在制作一个基于文本的信息亭,人们可以按作者,标题等进行搜索。现在我很难使用自助服务终端添加书籍。我可以添加所有信息,但在我输入每本书的信息后,我得到一个No Such Element异常。不确定这意味着什么,任何帮助都会受到赞赏。

import java.util.Scanner;
import java.util.ArrayList;

public class Books{

public static void main(String[] args) {
    //Part 1: Initialize the MIS

    //Part 1.1: Open Scanner
    Scanner keyboard = new Scanner(System.in);

    //Part 1.2: Build a store
    Store newstore = new Store();

    //Part 1.3: Build an array of books
    final int MAX_NUM_BOOKS = 500;
    Books[] list_book = new Books[MAX_NUM_BOOKS];

    //Part 1.4: Prompt for the initial book count 
    System.out.println("How many books do we have?");
    int bookcount = keyboard.nextInt();

    //Part 1.5: Builds the books
    for(int i=0; i<bookcount; i++) list_book[i] = new Books();

    //Part 1.6: Create switch menu
    boolean exit = false;
    while(!exit){
        System.out.println("Please make a selection: ");
        System.out.println("To search by Title (0)");
        System.out.println("To search by Author's Last name (1)");
        System.out.println("To search by Author's First name (2)");
        System.out.println("To search by ISBN (3)");
        System.out.println("To add a book to the inventory (4)");
        System.out.println("To exit (5)");
        int option = keyboard.nextInt();

    switch(option){
    case 0:System.out.print("Please enter the title of the book you are looking for: ");
    String title = keyboard.next();
    results(Book.TitleSearch(title));
    break;

    case 1: System.out.print("Please enter the last name of the author you are looking for: ");
    String auth_last = keyboard.next();
    results(Book.Auth_LastSearch(auth_last));
    break;

    case 2:System.out.print("Please enter the first name of the author you are looking for: ");
    String auth_first = keyboard.next();
    results(Book.Auth_FirstSearch(auth_first));
    break;

    case 3:System.out.print("Please enter the ISBN of the book you are looking for: ");
    String ISBN = keyboard.next();
    results(Book.ISBNSearch(ISBN));
    break;

    case 4:addBook(newstore);
    break;

    case 5: exit=true;
    }//switch
    }//while

    //Part :Close Input
    keyboard.close();
}
//Part 2: Add Books to the Store
private static void addBook(Store newstore) {
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter title of the book: ");
    String title = keyboard.next();

    System.out.print("Enter Author's first name: ");
    String auth_first_nam = keyboard.next();

    System.out.print("Enter Author's last name: ");
    String auth_last_nam = keyboard.next();

    System.out.print("Enter the ISBN: ");
    String isbn = keyboard.next();

    Book book = new Book(title, auth_first_nam, auth_last_nam, isbn);
    Store.addBook(book);

    keyboard.close();
}

private static String results(String titleSearch) {
    return Book.output();

}

}
//Build each individual book
class Book{
private static String title;
private static String auth_first_nam;
private static String auth_last_nam;
private static String isbn;
private static String bookinfo;

public Book(Scanner keyboard){
    System.out.println("Enter the book Title: ");
    title=keyboard.next();

    System.out.println("Enter in the Author's Last Name: ");
    auth_last_nam=keyboard.next();

    System.out.println("Enter in the Author's First Name: ");
    auth_first_nam=keyboard.next();

    System.out.println("Enter the ISBN number: ");
    isbn=keyboard.next();       
}

public Book(String title, String auth_first_nam, String auth_last_nam, String isbn) {

}

public static String output() {
    bookinfo=(title+" by " + auth_first_nam + " " + auth_last_nam + ". ISBN: " +isbn);
    return bookinfo;
}

public static String ISBNSearch(String iSBN2) {
    return isbn;
}

public static String Auth_FirstSearch(String auth_first) {
    return auth_first_nam;
}

public static String Auth_LastSearch(String auth_last) {
    return auth_last_nam;
}

public static String TitleSearch(String title2) {
    return title;
}

public String getTitle() {
    return title;
}

public String GetAuthorLast(){
    return auth_last_nam;
}

public String GetAuthorFirst(){
    return auth_last_nam;
}

public String getISBN() {
    return isbn;
}
}
class Store{
private static ArrayList<Book> books;
private static Book book;

public Store(){
    books = new ArrayList<Book>();
}

public static void addBook(Book Book){
    if(book==null){
        System.out.println("I'm sorry, but you didn't enter in any information.");
        return;
    }
    else{
    books.add(Book);
    }
}

public Book getTitle(String title){
    for(int i=0;i<books.size(); i++)
    {
        if(books.get(i).getTitle().equals(title))
        {
            book=books.get(i);
        }
    }
    return book;
}

public ArrayList<Book> findAutLast(String aut_last_nam)
{
ArrayList<Book> found = new ArrayList<Book>();
for(int i=0; i<books.size(); i++)
{
    Book current = books.get(i);

    if(current.GetAuthorLast().indexOf(aut_last_nam) >=0)
    {
        found.add(current);
    }
}
return found;
}

public ArrayList<Book> findTitle(String title){
ArrayList<Book> found = new ArrayList<Book>();
for(int i=0; i<books.size();i++)
{
    Book current = books.get(i);

    if (current.getTitle().indexOf(title) >=0)
    {
        found.add(current);
    }
}
return found;
}
public ArrayList<Book> findISBN(String isbn)
{
ArrayList<Book> found = new ArrayList<Book>();
for(int i=0; i<books.size(); i++)
{
    Book current = books.get(i);

    if(current.getISBN().indexOf(isbn) >=0)
    {
        found.add(current);
    }
}
return found;
}
}

对不起,有一些死密码和松散的目标仍然被捆绑。我对以前从未见过的No Element非常好奇。在案例4中使用Switch后,它发生在第35行。

2 个答案:

答案 0 :(得分:1)

问题实际上在这个方法中:

private static void addBook(Store newstore) {
    ...

    keyboard.close();
}

关闭keyboard的{​​{1}}个实例。

您调用Scanner的循环正在使用addBook扫描程序,下次循环调用keyboard失败,因为扫描程序已关闭。

我通过运行代码推断出这一点,并观察以下异常:

keyboard.nextInt()

值得注意的是,第36行是

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:838)
    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 Books.main(Books.java:36)

我在运行选项4(添加一本书)后才点击此异常。

如果您从int option = keyboard.nextInt(); 方法中删除keyboard.close(),您的程序将不会再崩溃。

我不希望您的程序正常运行,因为您还有其他一些错误(误用addBook()字段和方法),但它不会再抛出static

作为提示(因为这似乎是家庭作业),我在你的程序中看到的其他问题:

  • NoSuchElementException的静态字段(因此您希望在图书上定义的属性不会与对象保持一致)
  • 静态方法之子Book(类似于上文)
  • 未能使用适当的构造函数在Book中设置字段(并在不适当的构造函数中重复使用addBook中的代码)
  • 创建Book代替您可能想要的内容,这是Books[](或者由于Book[]
  • 中有ArrayList<Book> books,这可能是无关紧要的

答案 1 :(得分:0)

虽然很难分辨,因为你没有提供堆栈跟踪,但我可以看到一个问题,即何时访问ArrayList中的元素。通过盯着你的代码很难说,但我会先修复这个

private static String title;
private static String auth_first_nam;
private static String auth_last_nam;
private static String isbn;
private static String bookinfo;

您拥有static所有这些字段。这意味着,对于您创建并添加Book个图书的每个ArrayList,每个图书都将具有完全相同的title, auth_first_nan, auth_last_nam, isbn, and bookinfo。这可能会导致程序出现问题。虽然可能不会导致NoSuchElementException,但我仍然可以解决这个问题。

例如,如果您首先添加标题为“StackOverflow”Book,则添加标题为“StackExchange”的Book ,第一个Book的{​​{1}}也变为“StackExchange”。这意味着,如果您尝试通过title “StackOverflow”访问图书,您将永远找不到它

将所有字段更改为不包含title

static

查看 Java tutorial for static keyword