gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:05:26 25 4
gpt4 key购买 nike

我正在尝试编写一个模拟小型图书馆查询系统的程序,但我不断收到相同的错误。

错误如下:

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”类。

这是目录的文本文件:

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+");

关于java - 模拟小型图书馆查询系统时出现NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46464121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com