Ok so i have an assignment I'm working on where i have to write a program that has to:
1. Read the elements of a book(id, title, isbn, author, genre) from a text file.
2. Place these elements into a Book object.
3. Place these Book objects into a Book type array.
4. Prompt user to enter a an id for a Book and program should reply with all information about the book(ex: User enters "12345" and program should print to screen: Id: 12345, Title: Jimmy Cracked Corn, ISBN: 124512345, Author: JohnDoe, Genre: Fiction)
Now i am stuck on the part where i have to read in more and place more than one book object into the Book array. Here is my code so far.
Here is my code for the book class also:package assg2_Harris; import java.util.*; import java.io.*; public class FileInput extends Book{ public static void main(String[] args) { Scanner inStream = null; Book[] Catalog = new Book[100]; int count = 0; try { inStream = new Scanner(new File("catalog.txt")); } catch(FileNotFoundException x) { System.out.println("File not found."); } while(inStream.hasNextLine()){ Catalog[count] = new Book(inStream.nextInt(),inStream.next(),inStream.nextInt(),inStream.next(),inStream.next()); System.out.println(Catalog[count]); count++; } inStream.close(); System.out.println(Catalog[count]); } }
And also my txt file looks like this:package assg2_Harris; import java.util.*; import java.io.*; public class Book { private int bookid; private String bookname; private int isbn; private String author; private String genre; public Book(){ bookid = 0; bookname = ""; isbn = 0; author = ""; genre = ""; } public Book(int id, String title, int i, String name, String genre){ bookid = id; bookname = title; isbn = i; author = name; this.genre = genre ; } public int getBookId(){ return bookid; } public String getBookname() { return bookname; } public int getIsbn() { return isbn; } public String getAuthor() { return author; } public String getGenre() { return genre; } public String toString(){ return ("Book id: " + bookid + ", Title: " + bookname + ", ISBN: " + isbn + ", Author: " + author + ", Genre: " + genre); }
12345 Emma 0111111111 Rowling Fiction
15678 Timmy 135378564 Jesus NonFiction
Now i'm having a problem with this line file input class:
Catalog[count] = new Book(inStream.nextInt(),inStream.next(),inStream.n extInt(),inStream.next(),inStream.next());
The print statement is under it to see whats going on inside the array.
Anyway when I try to run my program this is what happens:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at assg2_Harris.FileInput.main(FileInput.java:24)
Book id: 12345, Title: Emma, ISBN: 111111111, Author: Rowling, Genre: Fiction.
Please help i have no idea why the lines inside the while loop aren't working for what i want to do....
Here's the link to my assignment prompt if you want to take a look at that.
https://blackboard.ecu.edu/bbcswebda...ssignment2.pdf