Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
For instance if my array is {34,12,56} first each number to be reversed that is {43,21,65} then the reversed array must be sorted and the final result will be : {21,43,65}.


I am getting few errors in while executing this code I dont know because of what I am getting those errors. Please check

What I have tried:

Java
import java.util.*;
public class ReverseSort
{
    public static int reversearray(int[] a, int n)
    {
         StringBuilder sb = new StringBuilder();
         for(int i=0; i<n ; i++)
         {
             a[i] = Integer.parseInt(sb.append(a[i]).reverse().toString());
             sb.setLength(0);
         }
         Arrays.sort(a);
         System.out.println(Arrays.toString(a));
    }

    //main
    public static void main(String args[])
    {
        int a[], n;
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        for(int i=0; i<n; i++)
        {

            a[i] = sc.nextInt();
        }
        System.out.println(a);
        ReverseSort.reversearray(a,n);
    }
}
Posted
Updated 21-Apr-21 23:46pm
v2
Comments
Richard Deeming 22-Apr-21 4:45am    
You seem to have forgotten to ask a question. What is the problem with the code you have written?
chaitanya chitturi 22-Apr-21 4:51am    
hey Richard, I am getting few errors while executing this code I don't know because of what I am getting those errors. Please check
Richard Deeming 22-Apr-21 4:53am    
If you want someone to help you fix those errors, then you need to provide the details of the errors. You can't simply say "I'm getting some errors" and expect us to work out what the problem is!
chaitanya chitturi 22-Apr-21 5:45am    
sorry I am new here that's why I post it like that.
CPallini 22-Apr-21 7:40am    
Do you realize that reversing the array has no effect on the final result?

Try this:
Java
public static void reversearray(int[] a, int n) // method does not return a value
{
     StringBuilder sb = new StringBuilder();
     for(int i=0; i<n ; i++)
     {
         a[i] = Integer.parseInt(sb.append(a[i]).reverse().toString());
         sb.setLength(0);
     }
     Arrays.sort(a);
     System.out.println(Arrays.toString(a));
}

public static void main(String args[]) }
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int a[] = new int[n]; // must create the array of n elements
    for(int i=0; i<n; i++)
    {

        a[i] = sc.nextInt();
    }
    System.out.println(Arrays.toString(a));
    reversearray(a,n); // no need for class name on the call to reversearay

}
 
Share this answer
 
Comments
CPallini 22-Apr-21 7:39am    
5.
Java
import java.util.*;
public class ReverseSort
{
public static int[] reversearray(int[] a, int n)
{
StringBuilder sb = new StringBuilder();
for(int i=0; i<n ; i++)
{
a[i] = Integer.parseInt(sb.append(a[i]).reverse().toString());
sb.setLength(0);
}

Arrays.sort(a);
System.out.println(Arrays.toString(a));

return a;
}
//main
public static void main(String args[])
{
int n;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
int a[] = new int[n];
for(int i=0; i<n; i++)
{

a[i] = sc.nextInt();
}

ReverseSort.reversearray(a,n);
}
}
 
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