This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Please help in this selection sort Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Please help in this selection sort" Watch "Please help in this selection sort" New topic
Author

Please help in this selection sort

Yazan Rashi
Greenhorn

Joined: Dec 03, 2010
Posts: 3
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

Joined: Oct 13, 2005
Posts: 38031
    
  22
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

Joined: Oct 13, 2005
Posts: 38031
    
  22
. . . and welcome to JavaRanch
Yazan Rashi
Greenhorn

Joined: Dec 03, 2010
Posts: 3
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

Joined: Oct 13, 2005
Posts: 38031
    
  22
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

Joined: Dec 03, 2010
Posts: 3
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

Joined: Oct 13, 2005
Posts: 38031
    
  22
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

Joined: Apr 10, 2009
Posts: 31
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.
 
 
subject: Please help in this selection sort
 
Similar Threads
Password creation using rng.nextInt()
Math.Random() query
Dsplaying the alphabet with a loop?
HELP~~
Two Dimensional Arrays