For this program I need to read in a list of data for a book (Title, Author, Price) from a text file to an arraylist in a separate class (Book). Ill be honest I just using Classes as objects within Java is just one of the things I cannot wrap my head around, and I dont have much experience with ArrayLists.
public void loadBook(String fn) throws IOException{
ArrayList<Book> books = new ArrayList<Book>();
Scanner infile = new Scanner(new InputStreamReader (new FileInputStream(fn)));
int num = infile.nextInt();
infile.nextLine();
for (int i=0; i<num; i++) {
String name = infile.nextLine();
String author = infile.nextLine();
Double price = infile.nextDouble();
Book c = new Book (name, author, price);
books.add(c);
}
infile.close();
}
And this is the code currently in the Book class.
public class Book extends Model {
public Book(String name, String author, Double price) {
String Name = name;
String Author = author;
Double Price = price;
}
And the file 'fn' contains this:
3
name
author
10.00
But loadBook still throws an error when reading in a file :@
Any help would be appreciated, thank you!!