Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Digit Ordering in Java

I was going through some random questions and encountered one that completely stumped me. Can anyone please suggest an efficient solution for the following,

Write a function to accept an integer and return a number made up of digits from the input number in ascending order. If the input is a negative, then change the order to descending.

Example Input: 48455 Output: 44558 Input: 15243 Output: 12345 Input: -15243 Output: -54321

This is the code I wrote and it works but I want to make use of collections or other methods to reduce the number of lines.

import java.util.*;
public class DigitOrdering {


    //Function
    public int orderDigits(int input) {
        int size;
        if(input >= 0){
            size = String.valueOf(input).length();  
        }else{
            size = String.valueOf(input).length() - 1;
        }

        int arr[] =  new int[size];
        int inputCopy = input;
        int i=0, num=0,j,temp;
        String stringNum="";
        boolean isNegative = false;
        if(inputCopy < 0){
            isNegative = true;
            inputCopy *= -1;
        }
        while(inputCopy > 0){
            arr[i++] = inputCopy % 10;
            inputCopy /= 10;
        }


        if(isNegative){
            for (i = 0; i < arr.length-1; i++){
                for (j = 0; j < arr.length-i-1; j++){
                    if (arr[j] < arr[j+1]){
                        temp = arr[j];
                        arr[j] = arr[j+1];
                        arr[j+1] = temp;
                    }
                }
            }
            for(int k: arr){
                //System.out.print(" "+k);
                stringNum += k ;    
            }
        }else{
            Arrays.sort(arr);
            for(int k: arr){
                //System.out.print(k);
                stringNum += k ;    
            }

        }
        num = Integer.parseInt(stringNum);
        //System.out.println(stringNum);
        if(isNegative){
            num *= -1;    
        }

        return num;
    }

    public void printOrderedDigits(int input) {
        System.out.println(orderDigits(input));
    }

    public static void main(String[] args) {

        if (args.length != 1) {
            System.out.println("Exactly 1 input required.");
            return;
        }
        try {
            int input = Integer.parseInt(args[0]);
            DigitOrdering obj = new DigitOrdering();
            obj.printOrderedDigits(input);
        } catch (NumberFormatException e) {
            System.out.println("Only integer allowed.");
        }
    }
}