This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Reading a list of books and users from a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading a list of books and users from a file" Watch "Reading a list of books and users from a file" New topic
Author

Reading a list of books and users from a file

Martin Bruce
Greenhorn

Joined: Oct 25, 2009
Posts: 7
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
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37940
    
  22
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

Joined: Oct 13, 2005
Posts: 37940
    
  22
And welcome to the Ranch
Martin Bruce
Greenhorn

Joined: Oct 25, 2009
Posts: 7
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

Joined: Oct 13, 2005
Posts: 37940
    
  22
No. BufferedReader is a perfectly good way to read from the file. It is simply that Scanner is easier to use.
Martin Bruce
Greenhorn

Joined: Oct 25, 2009
Posts: 7
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

Joined: Oct 13, 2005
Posts: 37940
    
  22
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

Joined: Oct 13, 2005
Posts: 37940
    
  22
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

Joined: Oct 25, 2009
Posts: 7
Am I on the right track??

Martin Bruce
Greenhorn

Joined: Oct 25, 2009
Posts: 7
Forgot something important to store the books and users I should use SortedLinkedList<E> class.
Martin Bruce
Greenhorn

Joined: Oct 25, 2009
Posts: 7

This works but I can't help thinking it was far to easy??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37940
    
  22
Well done. The whole idea about the design of a language is to make it easy to use.
Stanley Walker
Ranch Hand

Joined: Sep 23, 2009
Posts: 87
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading a list of books and users from a file
 
Similar Threads
generics extends question?
Generics
Reseting an Iterator
help with loop
SortedLinkedLists