• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading a list of books and users from a file

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Martin Bruce
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am I on the right track??

 
Martin Bruce
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This works but I can't help thinking it was far to easy??
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done. The whole idea about the design of a language is to make it easy to use.
 
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Maybe he went home and went to bed. And took this tiny ad with him:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic