Teraz sa ucim javu z jednej knihi, tak som to zobral ako ulohu a pokusil sa tonaprogramovat s obmedzenim, ze mozes zadavat iba kladne cele cisla. Program akceptuje 2 argumenty skladajucich sa iba z decimalnych cislic.
Tu je vysledny kod (bez komentarov aby bolo nad cim rozmyslat:
Kód:
class subLongDec {
	public static void main(String[] arguments){
		boolean sign = false;
		if(arguments.length != 2) {
            System.out.println("Wrong number of arguments");
            System.exit(1);
        }

        String cleannum1 = new String("");
        String cleannum2 = new String("");
        String temp = new String("");

        for(int j=0; j < arguments.length; j++){
            for(int i = 0; i < arguments[j].length(); i++){
                if(arguments[j].charAt(i) < 48 || arguments[j].charAt(i) > 57) {
                    System.out.println("Incorrect number format, enter correct decimal integer.");
                    System.exit(1);
                }
            }
        }

        temp = arguments[0];
        cleannum1 = temp;
        for(int i=0; i < temp.length(); i++){
            if(temp.charAt(i) == 48) cleannum1 = temp.substring(i+1);
            else break;
        }

        temp = arguments[1];
        cleannum2 = temp;
        for(int i=0; i < temp.length(); i++){
            if(temp.charAt(i) == 48) cleannum2 = temp.substring(i+1);
            else break;
        }

        System.out.println("n1: " + cleannum1 + " n2: " + cleannum2);
        
        int dif;
        if(cleannum1.length() > cleannum2.length()){
            dif = cleannum1.length() - cleannum2.length();
            for(int i = 0; i < dif; i++){
                cleannum2 = "0" + cleannum2;
            }
        }
        else if(cleannum2.length() > cleannum1.length()){
            dif = cleannum2.length() - cleannum1.length();
            for(int i = 0; i < dif; i++){
                cleannum1 = "0" + cleannum1;
            }
        }

        for(int i = 0; i < cleannum1.length(); i++){
            if(cleannum1.charAt(i) < cleannum2.charAt(i)){
                sign = true;
                break;
            }
            if(cleannum1.charAt(i) > cleannum2.charAt(i)) break;
        }
        if(sign == true){
            temp = cleannum1;
            cleannum1 = cleannum2;
            cleannum2 = temp;
        }

        int carr = 0;
        int c = 0;
        String sol = new String("");
        for(int i = cleannum1.length() - 1; i >= 0 ; i--){
            c = Integer.parseInt(cleannum1.substring(i,i+1)) - Integer.parseInt(cleannum2.substring(i,i+1)) - carr;
            if(c >= 0) {
                sol = String.valueOf(c) + sol;
                carr = 0;
            }
            else{
                sol = String.valueOf(10 + c) + sol;
                carr = 1;
            }
        }

        temp = sol;
        for(int i=0; i < temp.length() - 1; i++){
            if(temp.charAt(i) == 48) sol=temp.substring(i+1);
            else break;
        }

        if(sign == false) System.out.println("Sollution: " + sol);
        else System.out.println("Sollution: -" + sol);
        System.exit(0);		
	}
}