• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Sorting question

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have to make a program that will take fouyr strings, received through JOptionPane, and print to screen these 4 strings in both ascending and descending alphabetic sequence. I'm sure it's simple, but it's giving me trouble. Ideally what I'd like to do is read in the strings, store them, then pass those 4 strings as arguments to construct a "word" object. Then call methods on this object which will fulfill the requirements.

It's these methods that are giving me trouble. I DON'T want to use an array at all, so havings aid that, I gather I'd use the method string1.compareToIgnoreCase(string2), which makes perfect sense. However, how do I make this work properly without simply hardcoding in a multitude of if statements that address every possible permutation? Though that would work, I can't think of anything uglier. ;-)

Any help here would be greatly appreciated. I haven't posted any code, as all I have is literally the method headers with no implementation code within them! Thanks in advance!

shane
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shane,

How about you store the String objects in an ArrayList and use Collections.sort()?

For example: (this code must be compiled using jdk 1.5)


For more info on sorting using Collections, you can check out Sun's Java Tutorial:Lesson: Algorithms and Object Ordering .

Joyce
[ November 13, 2004: Message edited by: Joyce Lee ]
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is there a REASON why you don't want to use an array? To me, that's like saying "i want to build a house, but i don't want to use a hammer".

Sure, it can be done, but your handicapping yourself for no apparent reason.

I'm not saying you HAVE to use an array, or even that you SHOULD use one - i just don't understand your apparent predjudice against them...
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by fred rosenberger:
I'm not saying you HAVE to use an array, or even that you SHOULD use one - i just don't understand your apparent predjudice against them...



Because the homework assignment said don't use arrays?

If you know they're Strings and you know there's exactly four of them, a String[4] sounds like the right way to store the data. If there were different classes, an unknown or large number of them, Collections might be good. You can either use an unsorted Collection, like ArrayList, then sort it using methods on Collections, or use a sorted Collection, like TreeSet. Although it might be tricky to sort in both directions, with TreeSet.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ November 15, 2004: Message edited by: Mike Gershman ]
 
Shane McKenzie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I like arrays as much as the next guy, the reason I don't want to use them in this case is that I'm doing the exercises from a java book, and this question appear at the end of chapter 4, BEFORE arrays and vectors and lists are ever introduced, which implied to me that one should use another method (pardon the pun) to execute the task. I'm assuming that the reason is to give someone a really good appreciation of arrays when they're brought into play, which is in chapter 5, much to my consternation. ;-)
reply
    Bookmark Topic Watch Topic
  • New Topic