Calculate with numbers from different number systems This program calculates with numbers from different number systems and outputs the result in the desired number system.

You call it like that:

java Calculator &lt;operator&gt; &lt;number&gt; &lt;base&gt; &lt;otherNumber&gt; &lt;base&gt; (solutionBase)


Unfortunately it does not work with floating point numbers.

Example:

$java Calculator + 5234 7 FABCD43 16 3
200022201200110011


My gut feeling says me that my code is quite ugly but it doesnt tell me how to improve it. Do you have some hints for me how to make this more beautiful?

public class Calculator {
    public static void main(String[] args) {
        // display usage if user wants so
        if (args[0].contains("help")) {
            displayHelp();
            return;
        }

        // parse arguments
        String operator = args[0];
        String number1 = args[1];
        int baseOfNumber1 = Integer.parseInt(args[2]);
        String number2 = args[3];
        int baseOfNumber2 = Integer.parseInt(args[4]);
        int baseOfSolution = 0;
        if (args.length == 6) {
            baseOfSolution = Integer.parseInt(args[5]);
        }

        int number1Dec = randomBaseToDecimal(number1, baseOfNumber1);
        int number2Dec = randomBaseToDecimal(number2, baseOfNumber2);

        // calculate and print out
        int solutionDec = 0;
        if (operator.equals("+")) {
            solutionDec = number1Dec + number2Dec;
        } else if (operator.equals("-")) {
            solutionDec = number1Dec - number2Dec;
        } else if (operator.equals("x")) {
            solutionDec = number1Dec * number2Dec;
        } else if (operator.equals("/")) {
            solutionDec = number1Dec / number2Dec;
        }

        if (args.length == 6) {
            System.out.println(decimalToRandomBase(solutionDec,
              baseOfSolution));
        } else {
            System.out.println(solutionDec);
        }
    }

    private static int randomBaseToDecimal(String number, int base) {
        int result = 0;
        for (int i = 0; i &lt; number.length(); i++) {
            int digit = Character.getNumericValue(number.charAt(i));
            result = base * result + digit;
        }
        return result;
    }

    // works only till base 16
    private static String decimalToRandomBase(int number, int base) {
        StringBuilder finalNumber = new StringBuilder();
        while (number != 0) {
            if ((number % base) &gt; 9) {
                switch ((number % base)) {
                    case 10: finalNumber.append("A"); break;
                    case 11: finalNumber.append("B"); break;
                    case 12: finalNumber.append("C"); break;
                    case 13: finalNumber.append("D"); break;
                    case 14: finalNumber.append("E"); break;
                    case 15: finalNumber.append("F"); break;
                }
            } else {
                finalNumber.append(number % base);
            }
            number = number / base;
        }
        return new StringBuilder(finalNumber).reverse().toString();
    }

    private static void displayHelp() {
        System.out.println("This program calculates with numbers of different bases");
        System.out.println("Example: ");
        System.out.println("java Calculator + 34 5 554 6");
        System.out.println("You can also specify the base of the output number as the last argument:");
        System.out.println("java Calculator + 34 5 554 6 2");
    }
}
Main feedback has already been given by @AJNeufeld and this post is not about the performance of your program but rather other aspects.

You should try putting a check before you access an index in an array and slightly change your if block from this,

if (args[0].contains("help")) {


to,

if (args.length == 0 || args[0].contains("help") || args.length &lt; 5) {


as the former will run into ArrayIndexOutOfBoundsException if no argument is passed. Also it would be helpful to call the displayHelp() method in case no argument (args.length == 0) was passed so the user knows the usage of program.

Also, for safely accessing array indexes, you should put another OR condition args.length &lt; 5 which will ensure at least five parameters are passed, else again you may run into ArrayIndexOutOfBoundsException.

These checks should make the program a little more safer.