小さなライブラリの照会システムをシミュレートするプログラムを入力しようとしていますが、同じエラーが発生します。次のようにNumberFormatException小さなライブラリの照会システムをシミュレートするとき
エラーは次のとおりです。
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の入力を求め、カタログにリストされている場合は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);
}
}
それは私があなたのファイル内のスペースによって一つ以上のダッシュの出現をすべて交換することを提供し、私が正常に動作し、*と* –