我正在尝试键入一个模拟小型图书馆查询系统的程序,但我仍然遇到相同的错误。

错误如下:

Exception in thread "main" java.lang.NumberFormatException: For input string: "10001        Emma"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at assg4_user.BookDemo.readCatalog(BookDemo.java:51)
    at assg4_user.BookDemo.main(BookDemo.java:20)


我不确定如何处理。如果代码能够正确运行,则它将要求用户输入书ID,并且如果书ID在目录中列出,则它将输出Title,Author等。否则,将运行“ BookNotFoundException”类。

这是目录的文本文件:

Book ID-----Title------------------------ISBN#------------------Author---------------Fiction/Non-Fiction
10001-------Emma---------------------0486406482----------Austen---------------F
12345-------My_Life-------------------0451526554----------Johnson-------------N
21444-------Life_Is_Beautiful-------1234567890----------Marin-----------------F
11111--------Horse_Whisperer------1111111111------------Evans----------------F


这里的代码:

import java.io.*;
import java.util.*;

public class BookDemo {

    static String catalogFile = "C:\\Users\\John\\workspace\\DataStructuresAssingments\\catalog.txt";
    static Book[] bookArray = new Book[100];
    static int bookCount = 0;

    public static void main(String args[]) throws FileNotFoundException, IOException, BookNotFoundException {

        // Read Catalog
        readCatalog();

        System.out.println("Enter book id:");
        Scanner in = new Scanner(System.in);
        int bookId = Integer.parseInt(in.nextLine());
        while (bookId != 0) {
            bookSearch(bookArray, bookCount, bookId);
            bookId = Integer.parseInt(in.nextLine());

        }
        in.close();
    }

    /**
     * Reads catalog file using try-with-resources
     */
    private static void readCatalog() throws FileNotFoundException, IOException {
        String line;
        try (BufferedReader br = new BufferedReader(new FileReader(catalogFile));) {
            while ((line = br.readLine()) != null) {
                String[] str = line.split(" ");
                Book book = new Book(Integer.parseInt(str[0]), str[1], str[2], str[3], str[4].charAt(0));
                bookArray[bookCount] = book;
                bookCount++;
            }
        }
    }

    /**
     * Search Books
     */
    private static void bookSearch(Book[] bookArr, int bookCount, Integer bookId) throws BookNotFoundException {
        boolean found = false;
        for (int i = 0; i < bookCount; i++) {
            if (bookArr[i].getBookId().equals(bookId)) {
                System.out.println(bookArr[i]);
                found = true;
                break;
            }
        }

        if (!found) {
            throw new BookNotFoundException("Book ID:" + bookId + " Not Found!");
        }
    }
}




public class Book {

    private Integer bookId;
    private String bookName;
    private String bookISBN;
    private String bookAuthorLastName;
    private String bookCategory;

    public Book() { }

    public Book(Integer bookId, String bookName, String bookISBN, String bookAuthorLastName, char category) {
        this.bookId = bookId;
        this.bookName = bookName;
        this.bookISBN = bookISBN;
        this.bookAuthorLastName = bookAuthorLastName;

        if (category == 'F') {
            this.bookCategory = "Fiction";
        }
        else if (category == 'N') {
            this.bookCategory = "Non-Fiction";
        }
    }

    // Getter methods skipped for brevity

    @Override
    public String toString() {
        return "Book id:" + bookId + ", Title:" + bookName + ", ISBN:" + bookISBN + ", Author:" + bookAuthorLastName + "," + bookCategory;
    }
}




public class BookNotFoundException extends Exception {

    public BookNotFoundException() { }

    public BookNotFoundException(String message) {
        super(message);
    }
}

最佳答案

输入文件中是否有任何选项卡?或多个空格?也许您应该像这样拆分各行:

            String[] str = line.split("\\s+");

10-06 10:00