J'essaye de taper un programme qui simule le système de demande d'une petite bibliothèque, mais je continue d'obtenir la même erreur.NumberFormatException lors de la simulation du système d'interrogation d'une petite bibliothèque
L'erreur est la suivante:
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)
Je ne suis pas sûr de savoir comment faire face. Si le code devait s'exécuter correctement, il demanderait à l'utilisateur d'entrer un ID de livre et s'il est répertorié dans le catalogue, il produirait le titre, l'auteur, etc. Sinon, il exécuterait la classe "BookNotFoundException".
Voici le fichier texte pour le catalogue:
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
Et voici le code:
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);
}
}
Il fonctionne bien pour moi, pourvu que je remplace toutes les occurrences d'un ou plusieurs tirets par un espace dans votre fichier, * et * supprimer la ligne d'en-tête .. –