The Artima Developer Community
Sponsored Link

Java Answers Forum
what's wrong in this code????? urgently help required

1 reply on 1 page. Most recent reply: Aug 22, 2005 7:21 PM by fatboyy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
anoria

Posts: 3
Nickname: anorita
Registered: Aug, 2005

what's wrong in this code????? urgently help required Posted: Aug 21, 2005 2:01 AM
Reply to this message Reply
Advertisement
I WANT HELP ON THE FOLLOWING SORTING CODE.
PROBLEM IS THAT I WANT TO SORT THE CHARACTERS IN STRING.
1ST I CONVERTED STRING IN CHAR[] ARRAY THEN I TRY TO SORT CHAR[] ARRAY. AND THEN I CONVERTED THAT CHAR BACK INTO STRING .....

public class StringSort
{
public static String s1,s2;
public static int k;
public static char x,z;
public static char[] temp2;


public void sort() throws IOException
{
s1="tola";
k=s1.length();
char[] texttemp=s1.toCharArray();// t,o,l,a
for (int p=0;p<k;p++)
{
x=texttemp[p];
z=texttemp[p+1];
if (x<z)
{
temp2[k]=x; //t,o
temp2[k+1]=z;
}
else
{
temp2[k]=z;//o,t
temp2[k+1]=x;
}

}

s2=String.copyValueOf(temp2);//converting char to string
System.out.print(" sorted characters in string is "+s2);

}
public static void main(String[] args)
{



StringSort ss=new StrignSort();
try{
ss.StringSort();
}catch (IOException ex)
{System.out.print(" xxxxx ");}


}

}


fatboyy

Posts: 2
Nickname: fatboyy
Registered: Aug, 2005

Re: what's wrong in this code????? urgently help required Posted: Aug 22, 2005 7:21 PM
Reply to this message Reply
How about using the built-in Arrays class? There's typically something already written to do all the basic stuff like sorting.

import java.util.Arrays;
 
.....snip.....
 
        String test = "raw data";
        char[] temp = test.toCharArray();
        Arrays.sort(temp); 
        System.out.println(temp);

Flat View: This topic has 1 reply on 1 page
Topic: what's wrong in this code??? Previous Topic   Next Topic Topic: what's wrong in this code????? urgently help required

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use