Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Please help in this selection sort  RSS feed

 
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...
 
Marshal
Posts: 61720
193
  • 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
Marshal
Posts: 61720
193
  • 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
Marshal
Posts: 61720
193
  • 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
Marshal
Posts: 61720
193
  • 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.
 
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!