Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please help in this selection sort

 
Yazan Rashi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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...
 
Campbell Ritchie
Sheriff
Pie
Posts: 49460
64
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 49460
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to JavaRanch
 
Yazan Rashi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 49460
64
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Pie
Posts: 49460
64
  • Mark post as helpful
  • send pies
  • 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.
 
Alan Hampson
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic