0

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!!

3
  • 2
    "loadBook throws an error" --> Always include the complete error message in this type of questions. Commented Feb 13, 2013 at 16:04
  • The stack trace would be appreciated ! Commented Feb 13, 2013 at 16:04
  • I tried really hard to add the Stack trace, but for some reason StackOverflow had a problem with my code after and wouldnt let me save the edit, sorry Commented Feb 13, 2013 at 16:10

2 Answers 2

3

With this input:

3
name
author
10.00

This code

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);
 }

will set num to 3 and therefore execute the for loop 3 times. In each iteration the method nextLine() is called twice and nextDouble() once. But there are only 3 more lines in the file, so you are calling nextLine() too often. Try changing your input to

1      <-- number of listed books, not lines.
name
author
10.00
Sign up to request clarification or add additional context in comments.

3 Comments

Beautiful, Ill give it a run now
You could also leave out the number and test with if (!infile.hasNextLine()) break;
IT WORKS!!, You may have just saved me hours. Thank you so much (Apperently I have to wait 2 mins until your the correct answer)
1

Try make sure your text file is in your package in eclipse with .txt extention

//see what your default path is
System.out.println(System.getProperty("user.dir"));

//then use this code as continuation to the default path...for example if your path leads to your working directory
Scanner in = new Scanner(new FileInputStream("src/package_name/filename.txt"));

1 Comment

Yeah, the file is one of the few things with this code that didn't have a mystery problem. Cheers anyway tho

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.