• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting a string

 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
feda alshahwan
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to fix the problem if I can not pass by reference. Can you guide me please?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A method definition like this would work:
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The ranchers' diagnosis of your bug is correct.
However with mutable objects (even when marked final):

Your code could be simplified :
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic