Forums Register Login

Sorting a string

+Pie Number of slices to send: Send
I am trying to enter a string then output should be the string ordered suppose you enter CDAB output should be ABCD my code is not sorting the string ??

my code is as follows


Compiling 1 source file to C:\Documents and Settings\Moon\My Documents\NetBeansProjects\order1\build\classes
compile:
run:
Enter Your Name :
CADH
Name after reordering is:CADH
BUILD SUCCESSFUL (total time: 12 seconds)

[HENRY: Added Code Tags]
[ August 08, 2008: Message edited by: Henry Wong ]
+Pie Number of slices to send: Send

This method doesn't do anything. It assigns first and second different values but only within the swap method. It doesn't swap the references in the calling code.
+Pie Number of slices to send: Send
Look up the terms "call by reference" and "call by value". Youcould be a C programmer because your code assumes the method is called by reference.
To use your method logic, you'll have to pass the full string to the method along with the indexes of the chars to be swapped. Then build and return a new string with those chars swapped.
+Pie Number of slices to send: Send
How to fix the problem if I can not pass by reference. Can you guide me please?
+Pie Number of slices to send: Send
A method definition like this would work:
+Pie Number of slices to send: Send
The ranchers' diagnosis of your bug is correct.
However with mutable objects (even when marked final):

Your code could be simplified :
+Pie Number of slices to send: Send
 

Arrays.sort(charArray) ;



How does the OP learn how to code if he uses a built in method?
beginners need to do it the hard way first to learn how to loop and index.
I'm full of tinier men! And a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1330 times.
Similar Threads
sorting
method help
I can NOT compile first class below
Simple Logic Question
Reordering Alphabets
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 02:10:18.