aspose file tools*
The moose likes Beginning Java and the fly likes help with loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help with loop" Watch "help with loop" New topic
Author

help with loop

john mater
Greenhorn

Joined: May 08, 2009
Posts: 23
I'm having a special moment. I have a class user which holds info on a library customer. the class is very basic all it stores is the users name and how many books the user has on loan.. the condition is that a user cannot have more than 3 books at a time.. I cant the loop to function properly if a user has more than 3 books. please help. here is my code
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10354
    
    8

In your current code, consider books=4; In that case, your while loop will run for ever won't it?

Shouldn't you be having a User class and a Book class and the user containing a List of books? That way, if required you can even store the book information if required.
Have methods like assignBook and returnBook in the User class. The Book list.size() will give you the current books. Expose it with say another method in the User class like getCurrentBookCount.
Does this make sense?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
john mater
Greenhorn

Joined: May 08, 2009
Posts: 23
Sorry have a book class as well.. here it is. will fiddle around with your suggestions.. thankyou!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
That is not really a Book class; it is a linked list class. You would not expect a Book to have a reference to the book next to it, but you have. I think you should delete that field (next) and any references to it.
I also think you should delete the author field, because it is keeping the same information in two places. If you need the full name, try this:The names of your set and get methods ought to be the field name prefixed "set" or "get" with the appropriate capitalisation. You have one method called setSomething which actually behaves as a get method.

Now follow Maneesh's suggestions, which look very sensible.

And in your loop while (books > 3) . . . consider what will happen if you have 3 books, or 4 books, remembering that loop doesn't change the number of books anywhere.
john mater
Greenhorn

Joined: May 08, 2009
Posts: 23
Thanks for the help. I've neatened up both classes so that they only have properties and methods of their respective type. My problem is that I need help implementing maneesh's suggestions; How do I assign an object of one type (Book) to another (User), I have tried to add a new method and get an identifier expected error. Thanks
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10354
    
    8

John,
Think OOP. Check out the sample code skeleton below.
john mater
Greenhorn

Joined: May 08, 2009
Posts: 23
Thankyou Maneesh!!

I need to use a linked list (required in assignment specification...) but I guess if i change this for your array suggestion it should be similar? will try these alterations
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10354
    
    8

On the contrary, if you just substitute ArrayList with LinkedList even then the code need not change, because both of them implement the List interface.
You need to check with your professor, why a LinkedList is specified. That might mean some change in the assign and return method logic.
john mater
Greenhorn

Joined: May 08, 2009
Posts: 23
I am getting an error with..


if statement line needs a ) .. can't work it out.

Here is the specification.. it is just to use an alternate as practice against the familiar arraylist.

Task 2:
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
C. Snow
Concurrent Programming
Stephen J. Hartley
Java Gently
Judith Bishop
Petri Nets
Wolfgang Reisig
Finite Transition Systems
Andre Arnold
3
Emma Brown
Anna Smith
John Williams

The program should be able to store the information about books and users:
1. For each book, the information required is: the title, author’s name, whether it is on loan or not, and if it is on loan, who borrowed it. We assume that every book has only one author and that there are no two books in the library with the same title and author.
2. For each user, the library should know the first name, surname and the number of books currently held by the user. We assume that at any time a user can hold at most 3 books. We also assume that no two users share both the first name and the surname.

After the initial information has been read from the file, the librarian will be working with the program interactively. The program should display a menu on the screen offering a choice of possible operations, each represented by a lower case letter:

f - to finish running the program.
b - to display on the screen the information about all the books in the library.
v - to display on the screen the information about all the users.
i - to update the stored data when a book is issued to a user.
l - to update the stored data when a user returns a book to the library.

You should implement all the above operations.

Additional assumptions:
1. When f is selected, the program stops running and all the data are lost. The program could be extended to save all the data to a file, but this is not a part of the project!
2. To store books and users you should use SortedLinkedList<E> class. Books should be sorted in the ascending order of surnames of authors. You can assume that each author has only one surname and that it is always given after the first name(s) and/or initial(s). If there are several books by authors with the same surname, their order in the sorted list is not important. Users should be sorted in the ascending order of their surnames. If two users have the same surname then the first names should decide their order. You can assume that each user has exactly one first name and exactly one surname.
3. When a book is to be issued to a user, it must be checked whether the user is a valid user, and that the book is on the list of the books in the library. If not, an appropriate message should be displayed on the screen. If the request is a valid one, the program should check whether the book is on loan or not. If the book is currently on loan, a note to the user who is holding the book should be printed to a file informing that the book was requested by another user and should be returned as soon as possible. If the book is available, the stored information should be updated accordingly.
4. When a book is returned by a user, it must be checked whether the user is a valid user, and that the book is on the list of the books in the library and has been borrowed by this user. If not, an appropriate message should be displayed on the screen. If the request is a valid one, the stored information should be updated accordingly.

john mater
Greenhorn

Joined: May 08, 2009
Posts: 23
I think I'm getting towards where I need to be in terms of the User and Book classes however i get cannot find symbol variable size when i try to compile the following.


I thought List.size was part of the list package
john mater
Greenhorn

Joined: May 08, 2009
Posts: 23
sorted that
john mater
Greenhorn

Joined: May 08, 2009
Posts: 23
The other part of my task is to:

Task 1:
Derive a SortedLinkedList<E> class from the java.util.LinkedList<E> class in such a way that the items of a sorted linked list are sorted in ascending order.

I think I have a good idea about how to start this however my compiler is Bluej and it wont let me name my classes SortedLinkedList etc (invalid class name).. am I being really stupid? please help
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
Get rid of BlueJ.

Have you already got a SortedLinkedList file anywhere? You will get problems if you have two classes with the same name.
john mater
Greenhorn

Joined: May 08, 2009
Posts: 23
no I haven't got another file or class. I won't use bluej but have always learnt with it. could you recommend a program to write with, i have tried with notepad but find it hard to format my code well. Thanks
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10354
    
    8

John,
If I understand you correctly, the SortedLinkedList problem is not related to the user and book problem. In that case you might want to start a new thread for it.
http://faq.javaranch.com/java/UseOneThreadPerQuestion

There are many free text editors which help you format your code properly. TextPad and EditPlus are just a few of them. You might want to check them out.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
Agree with Maneesh about text editors. I've used NotePad++ NotePad2 and jEdit. All much much better than Microsoft Notepad.
john mater
Greenhorn

Joined: May 08, 2009
Posts: 23
I have developed a SortedLinkedList Class that should sort the Names of Users into Ascending author.. the Class compiles but when I try to run the test program I get the following error.



here is my Code:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
The class passed must implement the Comparable interface.
If you are using a Sorted linked list, it is probably better to use a while loop than a for loop. Use a test whether the present instance is greater than the next, and next != null.
john mater
Greenhorn

Joined: May 08, 2009
Posts: 23
My User class does implement Comparable.. Still gives the error.. am working on a while loop. also am trying to implement a screen class which takes a user input that gives the user of the program a list of operations.. Here is my code:



the code compiles fine but the problem I have is the if statement doesn't work. How do I make it so that the user input implement different operations.. switch statement?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
The error message tells you that User doesn't implement Comparable. Have you got your own Comparable file?
john mater
Greenhorn

Joined: May 08, 2009
Posts: 23
How do I make this code work?



the line System.exit(0) does not implement when choice = f, why is this? What other way is there to assign a string "f" to = choice? thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
That is because choice points to a Scanner, not to the String entered.
john mater
Greenhorn

Joined: May 08, 2009
Posts: 23
Thanks, as this is the case how do I invoke a method based on the string entered?
Brian Wolf
Greenhorn

Joined: Dec 01, 2008
Posts: 9
john mater wrote:Thanks, as this is the case how do I invoke a method based on the string entered?


If the goal, at this point, is to have the program exit based on a string entered by the user? Here's one example of how that could be accomplished:

Brian Wolf
Greenhorn

Joined: Dec 01, 2008
Posts: 9
john mater wrote:Thanks, as this is the case how do I invoke a method based on the string entered?


Now, if the goal is just to invoke any ole' method based on a string input by the user, then this minor modification to the above code can serve as an example of that.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
Why are you using the compareTo method rather than equals?
Brian Wolf
Greenhorn

Joined: Dec 01, 2008
Posts: 9
Campbell Ritchie wrote:Why are you using the compareTo method rather than equals?


This is a very good point, and, truthfully, the reason is simply that because I was originally going for a more complex solution that I thought would require compareTo. But, when I realized that it wouldn't work (or would take more time than I was willing to allocate to the project) I went with the quickest solution using compareTo, since I was already in that mindset, and already had "compareTo" typed in the editor.

Campbell brings up and excellent point in asking this question, since I have done nothing with this code that couldn't be done with equals.

For the original poster, here is the same example, but with utilization of the equals method rather than compareTo. This is in fact the better choice, since the equals method, per the Java6 API, implements the most discriminating possible equivalence relation on objects.


Thank you Campbell for pointing this out, and not letting me be lazy.
john mater
Greenhorn

Joined: May 08, 2009
Posts: 23
Thanks Brian,


This helps a lot however my code needs to be slightly different. The user needs to choose an option and each option will invoke a different method.. will a set of if else statements modelled around your code suffice?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
Brian Wolf wrote:. . . I went with the quickest solution . . .
Never say anything good about that reprobate Ritchie.

You haven't got the quickest method yet Look at this old post. Of course, she would have found the answer sooner if she had believed me first time!

Never never say == true or == false or similar. You might get nasty errors if you write = instead of ==. Anyway, you can delete every == true and replace every == false with a ! in the right location and the code's effect will be unchanged.
Brian Wolf
Greenhorn

Joined: Dec 01, 2008
Posts: 9
Campbell Ritchie wrote:You haven't got the quickest method yet Look at this old post.

Never never say == true or == false or similar. You might get nasty errors if you write = instead of ==. Anyway, you can delete every == true and replace every == false with a ! in the right location and the code's effect will be unchanged.


Thanks for the tip. I changed it. I also got another idea from that old post and switched my code from equals to equalsIgnoreCase, which does seem to be more appropriate for this situation.

Brian Wolf
Greenhorn

Joined: Dec 01, 2008
Posts: 9
john mater wrote:Thanks Brian,
This helps a lot however my code needs to be slightly different. The user needs to choose an option and each option will invoke a different method.. will a set of if else statements modelled around your code suffice?

Yes, a set of if else statements would work in your situation, if I understand it correctly. Another thing that might work for you, and make for cleaner code, is a switch statement. The only thing that may be a problem with using a switch is that it won't work with strings, so it just depends on the specifics of your program. With a switch you would have to use a byte, short, char, int, or enum. Enums are really cool, but may be more than you're willing to deal with at this point.

By the way. You don't have to use class Scanner either. The way you're getting user input it is fine, and probably preferred by some of the Java old-timers (which may be why your teacher or the author of your textbook uses it). I just use class Scanner to get user input form the console because that's the first way I learned to do it, so it's the method that comes most naturally to me. I also wanted to show that there is more than one way to get user input from the console. In my opinion it's good to know the BufferedReader class too, since it goes back all the way to JDK1.1. You can do a lot with class Scanner, but it's only available in JDK 1.5 or later. So, if you're working with anything pre-1.5 you'll need keep the console reading the way you have it. Oh... and just for fun I'll throw this in too: there is also class Console if you're on JDK6. I'm on JDK6 but I haven't tried it yet. However, just from skimming the API it looks like the simplest method yet. Now that I have discovered it I fully intend to try it out the next time I need to get input user input from the command line...which could be any second now...
angella cooper
Greenhorn

Joined: Dec 12, 2013
Posts: 1
Hi John, can you post your code once,I am having the same issue. can you post your classes and code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help with loop