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!
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.
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:
Finite Transition Systems
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.
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.
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.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop