Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Tor Ludgivsen
Greenhorn
Posts: 7
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! Im having a problem with my library system.

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



Full kode:
 
Greg Charles
Sheriff
Posts: 2993
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might be the problem:



and


It's always good to check the obvious stuff first!
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tor Ludgivsen
Greenhorn
Posts: 7
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50258
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic