Hi, Im stuck.
I want to have a method with:
- a parameter String s
- in the body it breaks the String s apart into chars then the chars
are put into alfabetical order.
- the return would give a String in alfabetical order.
In other words:
private String scramble(String s)
{
// put the chars from the string into alfatbetical order (black box)
return s;
}
Any suggestions?
Regards,
Michael (The Netherlands)
Patricia Shanahan - 22 Apr 2006 01:34 GMT
> Hi, Im stuck.
> I want to have a method with:
[quoted text clipped - 15 lines]
>
> Michael (The Netherlands)
Read the API documentation for each of these:
String method toCharArray. java.util.Arrays, String constructors.
Patricia
Michael - 22 Apr 2006 01:52 GMT
Thanks, these libraries are great :D
My solution:
private String scramble(String s)
{
int i;
String result ="";
char [] chararray;
chararray = s.toCharArray();
Arrays.sort(chararray);
for(i=0; i<s.length(); i++)
{
result+=chararray[i];
}
return result;
}
Patricia Shanahan - 22 Apr 2006 01:56 GMT
> Thanks, these libraries are great :D
> My solution:
[quoted text clipped - 13 lines]
> return result;
> }
You can make this better by taking another look at the String constructors.
Patricia
Greg R. Broderick - 23 Apr 2006 14:53 GMT
"Michael" <exquisite@zeelandnet.nl> wrote in news:1145667136.871120.118540
@z34g2000cwc.googlegroups.com:
> Thanks, these libraries are great :D
Depending on what sort of course this program is for, your professor may want
you to implement your own sort rather than using a library implementation.
Cheers
GRB

Signature
---------------------------------------------------------------------
Greg R. Broderick [rot13] terto@oynpxubyvb.qlaqaf.bet
A. Top posters.
Q. What is the most annoying thing on Usenet?
---------------------------------------------------------------------
Roedy Green - 22 Apr 2006 04:06 GMT
>private String scramble(String s)
>{
>// put the chars from the string into alfatbetical order (black box)
>return s;
>}
you could look on this as:
1. how do I convert String to char[] ( see
http://mindprod.com/applets/converter.html )
2. how do I sort the char[] (see http://mindprod.com/jgloss/sort.html
)
3. how do I convert the char[] back to a String ( see
http://mindprod.com/applets/converter.html )

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.