Click here to Skip to main content
15,868,004 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
public class sort_string 
{
	public static void main(String[] args)
    {	int j,temp=0;
        //String result = "";
        System.out.println("enter string\n");
    
        Scanner kbd = new Scanner(System.in);
        String input = kbd.nextLine();
        char[] arr=input.toCharArray();
        int count = 0;
        int [] intArray = new int[arr.length];
        for (char i : arr)
        {
          int m = Character.getNumericValue(i);
          intArray[count] = m;
          count += 1;
        }
        int len=intArray.length;
       
        	 boolean swapped;
             for (int i = 0; i < len - 1; i++) 
             {
                 swapped = false;
                 for (j = 0; j < len - i - 1; j++) 
                 {
                     if(intArray[j] < intArray[j + 1]) 
                     {
                         
                         temp = intArray[j];
                         intArray[j] = intArray[j + 1];
                         intArray[j + 1] = temp;
                         swapped = true;
                     }
                 }
      
                 // IF no two elements were 
                 // swapped by inner loop, then break
                 if (swapped == false)
                     break;
             
        
             }
             for(int a:intArray)
              System.out.println(a);
            	 //String str=Arrays.toString(intArray);
 }
}


What I have tried:

i want my output in a string like if input=2948 then output=9842.I'm using toString() but it doesn't take variable 'a'.Can anyone please help me in this?
Posted
Updated 21-Aug-18 21:02pm
v2
Comments
Mohibur Rashid 22-Aug-18 2:48am    
Your question is not clear. What problem are you having? Have you tired debugging?
Member 13954890 22-Aug-18 3:01am    
Code is working fine and i'm getting the output in individual manner like 9
8
4
2
i want to display my output in a single line like 9842.For this i'm trying to convert my int array into a string and display that string.But I'm not able to do so.what should i do then?

Just call Integer.toString(int) or String.valueOf(int)for each array item and append it to the output string:
Java
//String str;
String str = "";
for (int a:intArray)
    str += Integer.toString(a);
 
Share this answer
 
v2
Comments
Member 13954890 22-Aug-18 3:30am    
i got the output.i intialized it as str="" instead of writing null.Thanks a lot.
Jochen Arndt 22-Aug-18 3:35am    
Sorry, I forgot the initialisation (doing mainly C++ where a string object is automatically initialised as empty string).

Fine that you fixed that yourself.
you can tyr this

String str=intArray.toString();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900