• 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
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Please help in this selection sort

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I'm doing a program which is Selection Sort of an array of String... I want to get a 20 length string consist of letters from a-z and A-Z randomly...
I used String Buffer to get this random string but I'm having problems in doing the selection sort...
This is my code:

Any help is greatly appreciated...
 
Marshal
Posts: 80645
473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does the bit about < 0 mean? That looks possibly incorrect to me.
You should lose the te variable and replace it with array.length.
You should move as much code out of the main() method as possible.
 
Campbell Ritchie
Marshal
Posts: 80645
473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and welcome to JavaRanch
 
Yazan Rashi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used this loop in selection sort with String array and without using StringBuffer... and it worked fine with me...

The difference now is that I'm trying to generate Random String using StringBuffer... and it's something new to me Also, I don't know too much about Char arrays...
Help me please!
 
Campbell Ritchie
Marshal
Posts: 80645
473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you have posted now is different. You have added compareTo now, which won't work on a char.

Do a search for arithmetic on chars. You will find this, which may help with your chars.
 
Yazan Rashi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, this code I used it with String Array, not Char Array...
Thanks a lot for your replies... I really appreciate your help...
 
Campbell Ritchie
Marshal
Posts: 80645
473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Char array? Do you mean a char array? The two are different.
You will have to use different techniques for comparing chars from Strings; a reference type and a primitive behave differently.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd ask exactly what kind of "problems" are you having. Compile time? Runtime? Code throws an exception or doesn't run correctly?

A little more detail would help, because the code you posted here won't even compile for me.

Also confusing for me is what you're using the StringBuffer for. You load it with a random set of characters from array, but then you don't use it again.

You are performing operations on array, and those won't affect sb at all, even if they would work. Rethink your logic, then update your code accordingly.
reply
    Bookmark Topic Watch Topic
  • New Topic