模拟小型图书馆的查询系统时出现NumberFormatException

NumberFormatException when simulating the inquiry system of a small library

我正在尝试编写一个模拟小型图书馆查询系统的程序,但我总是遇到同样的错误。

错误如下:

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,如果它在目录中列出,那么它会输出书名、作者等。如果没有,它会 运行 "BookNotFoundException" Class.

这是目录的文本文件:

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

NumberFormatException 是一个 RuntimeException,因此如果未明确处理,它将 'slide by' 您的正常异常处理。所以用这样的 try-catch 包围你的 parseInt

try {
    bookId = Integer.parseInt(in.nextLine());
} catch (NumberFormatException e) {
    throw new BookNotFoundException();
}

我不会给你完整的解决方案或你的问题的逻辑。您收到此错误是因为您试图将字符串解析为整数 ("10001 Emma")。 Integer.parseInt("1001") 仅当传递的参数为 int 且不包含任何字符时才有效。

编辑

根据您的数据,您的 bookId 仅包含 int 数据。现在,如果您想强制用户仅输入 int 数据,请将 a 替换为下面的 b 部分:

一)

int bookId = Integer.parseInt(in.nextLine());

b)

int bookId;

    while(true)
    {
        try
        {
            bookId = Integer.parseInt(in.nextLine());
        }catch(NumberFormatException e)
        {
            System.out.println("Only integer input is accepted.");
            continue;
        }
        break;
    }

对第 27 行执行相同操作:bookId = Integer.parseInt(in.nextLine());

readCatalog 失败 parseInt。要进行调试,请将 parseInt 的参数打印到控制台(在本例中为 str[0])。如果它不是您所期望的,是时候打印整行并开始试验 String.split() 在 Java 中的工作方式以及如何让它正确解析您的行。

输入文件中有标签吗?或多个空间?也许你应该像这样拆分行:

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

我只能通过在 10001 后面放置一个制表符来重现您的错误。

10001⟶Emma     0486406482

是制表符)

你被一个 space 拆分了,所以 10001Emma 不是两个元素,而是一个数组元素,当然,它不能解析为整数.

您可以通过拆分一个或多个 whitespace characters 来解决此问题。由于 split() 接受正则表达式,你可以简单地写成这样:

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

哦,还有一些事情:

  • Bookclass中的属性全部以"book"开头。通常没有必要重复 class 名称。所以 idnameisbnauthorLastNamecategory 都可以。
  • 您可以使用 List 而不是数组。它更优雅,你不必在开始时声明大小。此外,不再需要 bookCount 变量,因为您可以使用 bookArray.size().
  • 获取大小
  • 有了Java8,就可以用函数式编程搜索书了

    private static void search(List<Book> books, Integer bookId) throws BookNotFoundException {
        boolean found = books.stream()
            .anyMatch(t -> {
                System.out.println(t);
                return Objects.equals(t.getBookId(), bookId);
            });
    
        if (!found) {
            throw new BookNotFoundException("Book ID: " + bookId + " Not Found!");
        }
    }