• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading a list of books and users from a file

 
Martin Bruce
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to be able to read a list of books and a list of users from a file.

The content of the input file needs to have the following format:

The first line contains an integer representing the number of books in the library.
The second and third line contains info about the books (second line containing the title and the third line containing the autor's name).
The fourth line contains an integer representing the number of library users.
This is then followed by the info about users (one line for every user).

An example of what I am trying to do is below:



Can anyone help me out, I'm very stuck on what to do!
 
Henry Wong
author
Marshal
Pie
Posts: 21008
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Bruce wrote:Can anyone help me out, I'm very stuck on what to do!


"stuck" implies that you actually started the homework problem. So, what have you done so far? And where exactly are you stuck?

Henry
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need the Scanner class to read from the file.
You can try matching your input with a regular expression to see whether it is a number or not, then you can count that many lines for the users, but this doesn't look particularly easy.

Try with Scanner first, and copy your file and alter it. Change it so you alternate book and (one) user, line by line. Then read that file with Scanner. Get that working. Then replace the numbers which represent how many users there are.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Martin Bruce
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers!

I was thinking BufferedReader would be the best way is this infact wrong and using the Scanner way would be better?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. BufferedReader is a perfectly good way to read from the file. It is simply that Scanner is easier to use.
 
Martin Bruce
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just don't get how I will make sure say a int is read for the first line, then a string for the 2nd and 3rd, then another int and then more strings.


You are asked to write a program to help a librarian in their daily work. Your program should read a list of books and a list of users from a file. The content of the input file should have the following form: The first line contains an integer representing the number of books in the library, and is followed by the information about the books (two lines for every book: one line containing the title and the second one containing the author’s name). The next line contains an integer representing the number of library users, followed by the information about the users (one line for every user with their first name and surname). Example file content is given below:

5
Concurrent Programming
Snow
Concurrent Programming
Stephen Hartley
Java Gently
Judith Bishop
Petri Nets
Wolfgang Reisig
Finite Transition Systems
Andre Arnold
3
Emma Brown
Anna Smith
John Williams


This is what I have been asked to do and then it goes on to say, my program
should then store the information about books and users.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try the hasNextInt method to see whether your next token matches an int of not.
For storing the information, you would have to create a class which encapsulates a book, and possibly its readers too.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need to create Book[] arrays and Reader[] arrays, you need the size of the arrays in advance. Whether you can predict the size from the integers in the file, well, can anybody tell? . . .
 
Martin Bruce
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I on the right track??

 
Martin Bruce
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgot something important to store the books and users I should use SortedLinkedList<E> class.
 
Martin Bruce
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This works but I can't help thinking it was far to easy??
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done. The whole idea about the design of a language is to make it easy to use.
 
Stanley Walker
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
might i suggest using a csv to store the book inventory.
lest say in the following format:

Number of Copies Book Title Book Author Book Users No Of Pages Book Release Date
5 Most Wanted Man John Le Carre user A;user B; User C 234 23-09-2009
8 Love in the time of Cholera Gabriel Garcia Marquez user A; user B; User C 234 23-09-2009

where the first row reads your data headers.
but they are some points to be noted:
1. data will be delimited by , hence the user has to be forced to not enter , anywhere within a single cell otherwise code will read it as the next cell. as you can see from above, that is exactly why users are delimited by ;

In order to read the above csv i have used the follwoing code:


where the book class has been defined as



some points to be noted:

1. any other details that you want to add other than the already lsited ones,(say for eg number of pages) it gets added to a hashmap as additional book information.
2. there exists some hardcoding for the cell headers. lest say if i change the header form number of copies to no of copies my code would fail.. any suggestions as to how i could avoid this.
3. and as i previously mentioned code would go haywire if user decided to add commas within a single cell.


I hope this fulfills your need.
Suggestion on the above code is welcome, i know i didnt put in exception handlers and i could have finetuned the code furthermore. please suggest how.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic