next up previous
Next: Fix the two problems Up: Part One Previous: Loopage!

Send in the second String

1.
Create another String with a creative name like name2 and initialize it with another name, like Samuel Clemens.
2.
Use the compareTo method to compare the two Strings. To compare two Strings, you invoke compareTo on one of the Strings and pass the other String as an argument. It's a little weird, but that's how it goes.

There are two problems with this approach to comparing names. First, it gets fooled by upper and lower case letters. So e e cummings and my friend k claffy would not appear in the right place in the alphabet. Second, it compares the first names first and the last names last, and it should be doing the opposite. (Although in my high school year book I actually convinced the other editors that our pages should appear in alphabetical order by first name, just for the sake of being different and weird. I don't know how I pulled that off).



Allen B. Downey
3/11/1998