Forums Register Login

Reading a list of books and users from a file

+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
And welcome to the Ranch
+Pie Number of slices to send: Send
Cheers!

I was thinking BufferedReader would be the best way is this infact wrong and using the Scanner way would be better?
+Pie Number of slices to send: Send
No. BufferedReader is a perfectly good way to read from the file. It is simply that Scanner is easier to use.
+Pie Number of slices to send: Send
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.

 
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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? . . .
+Pie Number of slices to send: Send
Am I on the right track??

+Pie Number of slices to send: Send
Forgot something important to store the books and users I should use SortedLinkedList<E> class.
+Pie Number of slices to send: Send

This works but I can't help thinking it was far to easy??
+Pie Number of slices to send: Send
Well done. The whole idea about the design of a language is to make it easy to use.
+Pie Number of slices to send: Send
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.
If we don't do the shopping, we won't have anything for dinner. And I've invited this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2253 times.
Similar Threads
Generics
SortedLinkedLists
help with loop
Reseting an Iterator
generics extends question?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 14:17:04.