File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes quick lookup, quick sort, mapping help needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "quick lookup, quick sort, mapping help needed" Watch "quick lookup, quick sort, mapping help needed" New topic
Author

quick lookup, quick sort, mapping help needed

Brandon Lowe
Greenhorn

Joined: Apr 16, 2013
Posts: 8
Greetings all,
I could use some help on a program I need to write. The link to the program's instructions is pasted below:
http://grail.cba.csuohio.edu/~matos/notes/cis-265/2013-spring/Homework/cis265-fall2013-hw04.pdf

Data required:
http://grail.cba.csuohio.edu/~matos/notes/cis-265/2013-spring/Homework/indians2013in.csv

Here is my thought.. Either created an arraylist or a treemap of Player objects and use this to created the data in the memory. A map would probably be better for sorting, but I'm not very good with them and I don't totally understand how they work. If anyone could give me a push in the right direction I would greatly appreciate it. Some of my code is pasted below:



Player class:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Welcome to the Ranch

I have added code tags to your code; you should always use them, and you can see how much better it looks
There is something worrying about seeing so many parameters in a constructor; you have to be very very careful about them.
It doesn’t say to use a tree map, nor an array list. I am not sure whether you ought to create your own data structure or use a ready‑made data structure. I would suggest you leave off coding for a bit. Get some paper and a pencil and an eraser (preferably a large one), and write down how you would find those data on paper. Also go through the Java Tutorials section. There are sorts of data structures ready‑made which you can use to link names to Player objects.
Brandon Lowe
Greenhorn

Joined: Apr 16, 2013
Posts: 8
Campbell Ritchie wrote:Welcome to the Ranch

I have added code tags to your code; you should always use them, and you can see how much better it looks
There is something worrying about seeing so many parameters in a constructor; you have to be very very careful about them.
It doesn’t say to use a tree map, nor an array list. I am not sure whether you ought to create your own data structure or use a ready‑made data structure. I would suggest you leave off coding for a bit. Get some paper and a pencil and an eraser (preferably a large one), and write down how you would find those data on paper. Also go through the Java Tutorials section. There are sorts of data structures ready‑made which you can use to link names to Player objects.


Thank you for your reply. I understand your concern about the constructor, but with so many data fields needing to be written for each Player object I couldn't think of another way to do it. The good news is my file reader does successfully read and write each Player object. I have tested it with the showData() method and the Player object is written correctly. So, next I'm thinking I need to implement a Map<K,V> interface, but I'm not sure exactly how to do it and the tutorials don't make much sense to me...
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Brandon Lowe wrote:I'm thinking I need to implement a Map<K,V> interface, but I'm not sure exactly how to do it and the tutorials don't make much sense to me...


Unless you're required to implement your own Map class, it'd be favorite to use a Map<K, V> that's already been implemented for you, such as HashMap.

What part of the tutorials didn't you understand?

Do you know what your K and V would be? That is, what will you be looking up (V), and by what property or properties will you be looking for it (K)?
Brandon Lowe
Greenhorn

Joined: Apr 16, 2013
Posts: 8
Here is one part I can't get working:



Here is my compareTo method:



I have gotten similar methods to work in the past and cannot figure out why it is not working this time.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Why can’t you pass the whole line to the constructor? Or create a (static) valueOf(String csvText) method? Both still can fall down dreadfully if there is the tiniest error in the csv file.
Brandon Lowe
Greenhorn

Joined: Apr 16, 2013
Posts: 8
Jeff Verdegan wrote:
Brandon Lowe wrote:I'm thinking I need to implement a Map<K,V> interface, but I'm not sure exactly how to do it and the tutorials don't make much sense to me...


Unless you're required to implement your own Map class, it'd be favorite to use a Map<K, V> that's already been implemented for you, such as HashMap.

What part of the tutorials didn't you understand?

Do you know what your K and V would be? That is, what will you be looking up (V), and by what property or properties will you be looking for it (K)?


So in the example of this program I need to write K would be lastName and V would be my Player objects?
Brandon Lowe
Greenhorn

Joined: Apr 16, 2013
Posts: 8
Campbell Ritchie wrote:Why can’t you pass the whole line to the constructor? Or create a (static) valueOf(String csvText) method? Both still can fall down dreadfully if there is the tiniest error in the csv file.

Do you mean pass the filereader's whole line to the constructor and have the constructor break it down into tokens and apply the tokens to the variables? If so then yeah I could do that but it's not a priority of mine since the filereader and makeNewPlayer method seem to be working fine.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Brandon Lowe wrote:
Here is my compareTo method:



I have gotten similar methods to work in the past and cannot figure out why it is not working this time.


When comparing objects' states (contents)--including Strings--you need to use equals(), not ==.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Brandon Lowe wrote:
Jeff Verdegan wrote:
Brandon Lowe wrote:I'm thinking I need to implement a Map<K,V> interface, but I'm not sure exactly how to do it and the tutorials don't make much sense to me...


Unless you're required to implement your own Map class, it'd be favorite to use a Map<K, V> that's already been implemented for you, such as HashMap.

What part of the tutorials didn't you understand?

Do you know what your K and V would be? That is, what will you be looking up (V), and by what property or properties will you be looking for it (K)?


So in the example of this program I need to write K would be lastName and V would be my Player objects?


Yes, if you're going to be storing and retrieving players by last name. Note that this only works if you know in advance that no two players will ever have the same last name. If that's one of the conditions of this exercise, then that's fine. Just be aware that a real-world implementation would require a different approach--such as mapping last name to a List of players with that last name.
Brandon Lowe
Greenhorn

Joined: Apr 16, 2013
Posts: 8
Jeff Verdegan wrote:
Brandon Lowe wrote:
Here is my compareTo method:



I have gotten similar methods to work in the past and cannot figure out why it is not working this time.


When comparing objects' states (contents)--including Strings--you need to use equals(), not ==.


Ok, thank you. Unfortunately, the code is still not working properly.



I've also placed a separate if statement to test if the method is even returning a value of 1 and it is not. I'm not sure what the problem is, but the method is not returning a value of 1 or 0..
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Brandon Lowe wrote:
Ok, thank you. Unfortunately, the code is still not working properly.



You need to review the docs for what compareTo() is supposed to return.

If you know that the last names won't be null, you can write your compareTo() as a single statement.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Do you need the players to be comparable to one another? Do they have a natural ordering? Can you write Comparator objects to sort them in terms of batting average, or similar?
In the Java Tutorials link I gave you earlier, there is a section about object ordering. That might be helpful. But you need to know why you are ordering things first.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Campbell Ritchie wrote:Do you need the players to be comparable to one another? Do they have a natural ordering? Can you write Comparator objects to sort them in terms of batting average, or similar?
In the Java Tutorials link I gave you earlier, there is a section about object ordering. That might be helpful. But you need to know why you are ordering things first.


Good point.

@Brandon: If you're just looking to put players into a HashMap and retrieve them by last name, then you don't need to worry about compareTo(). That's used for sorting. If you just want to be able to look up a player by last name from a HashMap, you'll need to override equals() and hashCode() on your Player class.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Jeff Verdegan wrote: . . . If you just want to be able to look up a player by last name from a HashMap, you'll need to override equals() and hashCode() on your Player class.
You certain? Don’t you only need overridden equals() and hashCode() on the “K”, which String already has overridden?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Campbell Ritchie wrote:
Jeff Verdegan wrote: . . . If you just want to be able to look up a player by last name from a HashMap, you'll need to override equals() and hashCode() on your Player class.
You certain? Don’t you only need overridden equals() and hashCode() on the “K”, which String already has overridden?


Grrr...

Yes, of course. Stupid mistake. Sorry for any confusion.
Brandon Lowe
Greenhorn

Joined: Apr 16, 2013
Posts: 8
Thank you again to those who have replied. I have made great progress on this program and now I'm just one step away from completing it.


And


The part that is not working is the method playerLookup. No matter what I try I get "Player not found."

Thanks again to all who have replied.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Why on earth are you putting String[]s as the “V”s in your Map? You asked earlier about a Map<String, Player>, which is what you should use. You have gone to all the trouble of getting battingAverage into the Player object; now all you need to do is use a method to get it back from the Player object.
As somebody else said, you will get problems if you have two Players with the same name.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
There is also something bad about the design for your uniqueAverages method.
If you are looking up "Cabrera, A" it will look for Cabrera, A. It cannot find Cabrera nor A Cabrera. Only Cabrera, A. So in order to find Cabrera, A, you must “put” "Cabrera, A", … The arguments must be exactly the same.
Brandon Lowe
Greenhorn

Joined: Apr 16, 2013
Posts: 8
Campbell Ritchie wrote:There is also something bad about the design for your uniqueAverages method.
If you are looking up "Cabrera, A" it will look for Cabrera, A. It cannot find Cabrera nor A Cabrera. Only Cabrera, A. So in order to find Cabrera, A, you must “put” "Cabrera, A", … The arguments must be exactly the same.


What is wrong with the uniqueAverage method? I only need each unique AVG with no repeats so I figured a set would work well here.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Populating your set every time you call the method.
Using the no 17 element in the array rather than a getBattingAverage() method.
Using Strings instead of numbers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: quick lookup, quick sort, mapping help needed