File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Cattle Drive and the fly likes Sorting it out Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Sorting it out" Watch "Sorting it out" New topic
Author

Sorting it out

Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
I am sorry to say this, but this assignment has totally kicked my butt. I keep spinning my wheels in the sand trying to find a way to access the elements in my ArrayList in a manner where I can use String operations on them. I just can't wrap my mind around this one. Since an ArrayList is an object itself, and that object is what gets passed into sort, how do I write code which will access the index of the String character after the space? That is the only way I can I think of to get the list sorted by last name without changing it. Any help or pointing in the right direction would be greatly appreciated by my family as I am getting quite grumpy because I can't solve this!
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2
Think about how much you've done so far. You now have a working knowledge of basic input and output in Java and how to work with collections and iterators.
To figure out the last name sort business, I'd suggest rereading the first couple of posts in the SortNames thread started by Matthew Phillips for some ideas.
Somewhere in all of this someone posted the URL for the Sun Java Tutorial trail on Collections. It's a great overview of how this stuff all fits together (written by the guy who designed and coded it!). I realize it can be overwhelming. Take a look at the example of how to do a custom sort (using an interface) on the page called Object Ordering. This short little example should give you some ideas to play around with.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
If you "put" Strings into the ArrayList, you can "get" Strings out of the ArrayList.

"how do I write code which will access the index of the String character after the space? "

method in the String class


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
Okay, thanks. I figured out how to print out all of the last names in my ArrayList using get() and some String operations. I am so close I can taste it. Now I am trying to pass a substring into sort as the Comparator, but I am getting this error message:
Incompatible type for method. Explicit cast needed to convert Comparator to java.util.Comparator.
What is going on here? Have I strayed? Lets play you're getting warmer, no, now you're getting colder....
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
Originally posted by Marilyn deQueiroz:
[B]If you "put" Strings into the ArrayList, you can "get" Strings out of the ArrayList.

Thanks Marilyn! How was your vacation? Welcome home!!!
I was thinking for some reason, that an ArrayList was a complete unit and since it was read in from another file, it had to be treated as a whole. Don't know where I got that idea, but then I stumbled onto the get() method which had eluded me until now, and I was able to write code to print each last name. Now I'm stuck again trying to plug it all into sort()!
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195

To figure out the last name sort business, I'd suggest rereading the first couple of posts in the SortNames thread started by Matthew Phillips for some ideas.
I keep going back to that, hoping it will make more sense each time I read it......
Somewhere in all of this someone posted the URL for the Sun Java Tutorial trail on Collections. It's a great overview of how this stuff all fits together (written by the guy who designed and coded it!). I realize it can be overwhelming. Take a look at the example of how to do a custom sort (using an interface) on the page called Object Ordering. This short little example should give you some ideas to play around with.
I have been poring over this tutorial, but it is way over my head in many places, I'm afraid. I just have to keep plugging away at it and hope that it will all click eventually. Right now I feel like an idiot!
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
You feel like an idiot, with you background. Gee where does that leave me with my nearly 25 years of IT experience !!.
Thanks for suggesting than I'm a moron
Keep on plugging away. You will get there. Good luck.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Originally posted by Carol Murphy:
I keep going back to that, hoping it will make more sense each time I read it......
I have been poring over this tutorial, but it is way over my head in many places, I'm afraid. I just have to keep plugging away at it and hope that it will all click eventually. Right now I feel like an idiot!

Been there, done that.
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2
Originally posted by Carol Murphy:
Now I am trying to pass a substring into sort as the Comparator, but I am getting this error message:
Incompatible type for method. Explicit cast needed to convert Comparator to java.util.Comparator.

That's the error you should expect in this situation. It's the compiler's way of saying that you're trying to put a square peg into a round hole.
The method is expecting an object that is an instance of a class that implements the Comparator interface. You're sending it an instance of String. String doesn't implement Comparator. Therefore Java's attempt to upcast your String to Comparator at the point of the method call fails.
Write a class that implements Comparator and send an instance of it to the method.
[This message has been edited by Michael Matola (edited July 06, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting it out