Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting it out

 
Carol Murphy
village idiot
Bartender
Posts: 1202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1817
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Carol Murphy
village idiot
Bartender
Posts: 1202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1817
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic