简体   繁体   中英

Sorting a user input from High to low gives wrong answer in java

Below is my program which takes user input and then sorts it from higher to lower:

This program takes the input from the user as an Integer and then converts it into a string and then passes it to the sort(int[] sort) method.

public class SortLtoS { 

void convert(int n)                     // converts int to string
{
    String temp = Integer.toString(n);
    int [] sort = new int[temp.length()];
    for(int i=0;i<temp.length();i++)
    {
        sort[i] = temp.charAt(i)-0; 

    }

    sort(sort);
}
void sort(int[] sort)           // bubble sort the integer array
{

    int temp=0;         
    for(int j=0;j<sort.length;j++)
    {
        for(int k=1;k<(sort.length-j);k++)
        {
            if(sort[k-1]>sort[k])
                temp=sort[k-1];
            sort[k-1]=sort[k];
            sort[k]=temp;
        }

        for(int i=sort.length-1;i>=0;i--)
            System.out.println(sort[i]);
    }


}


public static void main(String[] args) {

    System.out.println("Enter the number to sort");
    Scanner sc = new Scanner(System.in);
    int a = sc.nextInt();
    SortLtoS t = new SortLtoS();
    t.convert(a);


  }

 }

Now when I am providing input as 76878 it should give output as 88776 but it's giving output as

55 56 55 56 54 55 55 56 55 56 55 55 56 56 55 55 55 56 56 56 55 55 56 56 56

What am I doing wrong here?

What you want is not sort[i] = temp.charAt(i)-0; , it should be sort[i] = temp.charAt(i)-'0'; , you want to minus the encoding of character '0', not the value of 0. Though I don't think you need this step, because originally from your main method, you can just scanner in a string, why did you scan in a int, and convert it a string? Just to make sure it's a valid number?

An other issue of you code is, you should enclose all three statements of your swapping logic by curly braces of the if statement. Move your printing statements of array sort out of the sorting logic.

Your sort function should be like this:

void sort(int[] sort) // bubble sort the integer array
{
    int temp = 0;
    for (int j = 0; j < sort.length; j++) {
        for (int k = 1; k < (sort.length - j); k++) {
            if (sort[k - 1] > sort[k]) {
                temp = sort[k - 1];
                sort[k - 1] = sort[k];
                sort[k] = temp;
            }
        }
    }

    for (int i = sort.length - 1; i >= 0; i--)
        System.out.println(sort[i]);

}

Its working by using collection frameworks

public class SortLtoS  {

    public static void main(String[] args) {
        System.out.println("Enter the number to sort");
        Scanner sc = new Scanner(System.in);
        Integer number  = sc.nextInt();
        List<String> list = new ArrayList<String>();
        Integer rem;
        Integer n = 0;
        do {
            rem = number % 10;
            number = number / 10;

            list.add(rem.toString());
        } while (number > 0);

        Comparator comparator = Collections.reverseOrder();
        Collections.sort(list, comparator);
        String[] str = new String[list.size()];
        str = list.toArray(str);
        StringBuilder builder = new StringBuilder();

        for (String s : str) {
            builder.append(s);
        }
        System.out.println(builder.toString());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM