No Such Element Exception for Book Store Kiosk

205 views Asked by At

So this is a rough work in progress. I am making a text based information kiosk where people can do a search by author, title, etc. Right now I am having a hard time adding book using the kiosk. I can add all the info, but after I enter the information per book, I get a No Such Element exception. No sure what that means and any help would be appreciated.

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

Sorry, there's some dead code and loose ends still being tied up. Just really curious about the No Element as I've never seen it before. It happens at line 35 after using the Switch for case 4.

2

There are 2 answers

3
Mark Elliot On BEST ANSWER

The issue is actually in this method:

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

    keyboard.close();
}

Where you close the keyboard instance of a Scanner.

The loop in which you call addBook is making use of the keyboard scanner, and the next time through the loop calls keyboard.nextInt() fails, because the scanner is closed.

I deduced this by running your code, and observing the following exception:

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)

Notably, line 36 is

int option = keyboard.nextInt();

And I only hit this exception after running option 4 (add a book).

If you remove keyboard.close() from your addBook() method, your program will no longer crash.

I don't expect your program to work, as you have a number of other errors (misuse of static fields and methods), but it will no longer throw a NoSuchElementException.

As a hint (since this appears to be homework), the other problems I'm seeing in your program:

  • static fields for Book (so the properties you wish to define on a book won't persist with the object)
  • static method son Book (similar to above)
  • failure to set fields in Book using the appropriate constructor (and repeating code from addBook in an inappropriate constructor)
  • Creating a Books[] instead of what you likely want, which is a Book[] (or maybe this is irrelvant since you have an ArrayList<Book> books in Store
3
Paul Samsotha On

Though it's really hard to tell because you haven't provided the stacktrace, I could see a problem arrising when to access an element in the ArrayList. It's hard to tell just by staring at your code, but I would start by fixing this

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

All of these fields you have as static. That means that for every Book you create and add the ArrayList of books, every single one will have the exact same title, auth_first_nan, auth_last_nam, isbn, and bookinfo. That could cause a problem in the program. Though probably doesn't cause the NoSuchElementException, I would still make this fix.

For example, if you first add a Book with title "StackOverflow", then you add a Book with title "StackExchange", the first Book's title also become "StackExchange". That means that if you ever try and access a Book by title "StackOverflow", you would never find it

Change all the fields to not include the static

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

Check out Java tutorial for static keyword