aspose file tools*
The moose likes Beginning Java and the fly likes Having problem when i sort out data from hashmap. Variables are mixing! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Having problem when i sort out data from hashmap. Variables are mixing!" Watch "Having problem when i sort out data from hashmap. Variables are mixing!" New topic
Author

Having problem when i sort out data from hashmap. Variables are mixing!

Tor Ludgivsen
Greenhorn

Joined: Dec 15, 2012
Posts: 7

Hello! Im having a problem with my library system.

the p.name and p.isbn are mixing up.



Full kode:


"None are more hopelessly enslaved than those who falsely believe they are free" - Johann Wolfgang von Goethe
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2864
    
  11

This might be the problem:



and


It's always good to check the obvious stuff first!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8418
    
  23

Tor Ludgivsen wrote:Hello! Im having a problem with my library system...

Tor,

1. For future reference, try to only post code that is relevant to your problem; it saves us from having to plough through a lot of irrelevant information and will help you get answers quicker.

And since Greg's already shown you your actual error, here are a few general tips:

2. Don't use Strings for anything except strings of characters; and the only field in your Person class that meets that criterium is name. For more information, see this page.

3. Avoid using single-character names for anything except loop variables. For example:
int a = personm.size();
Why not:
int size = personm.size();
(or the equivalent in Norwegian)?
Anything you can do to make your programs more readable is a good thing.

4. Your sort method is a good example of overthinking (and don't worry; it's a common beginner's mistake ):
First: Lists are usually better than arrays; and you can create a List directly from a Collection.
Second: You can sort an ArrayList just as quickly with Collections.sort() as you can an array with Arrays.sort().
Third: You can sort objects any way you like with a Comparator (java.util.Comparator). If you haven't heard about them yet, I suggest you read up on them.
Fourth: Get into the habit of using for-each loops. They save you all that mucking about with Iterators.

Put that all together and you get something like:
HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Tor Ludgivsen
Greenhorn

Joined: Dec 15, 2012
Posts: 7

Greg Charles thanks a lot. and i did look at it, but did not know that it matters the order i put data in.

Winston Gutkowski. thanks a lot, will follow your advices and try the new stuff you told me. is there some place on the forum where you can post your product, and just get advices on how to improve things?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40041
    
  28
Tor Ludgivsen wrote: . . . is there some place on the forum where you can post your product, and just get advices on how to improve things?
Yes. Here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Having problem when i sort out data from hashmap. Variables are mixing!