jeudi 25 janvier 2018

Android Java if statement in for loop crashing app

I'm new to Android and Java and there must be something I'm missing because I think this should work fine. Up to this point my code does what I expect and my variables all have the values I expect but as soon as it hit the loop it crashes. I just need the loop to do something different for the first iteration.

        for (int i = 0; i == Years; i++){
        if (i == 0) {
            c.setTime(fPayDate);
            balAr.add(balance);
            payAr.set(i, BigDecimal.valueOf(0));
            intAr.set(i, BigDecimal.valueOf(0));
            prinAr.set(i, BigDecimal.valueOf(0));
            i++;
        }else {
            c.add(Calendar.DATE, 7);
            balAr.add((balAr.get(i).subtract(prinAr.get(i))));


            if (balance.subtract(payment.subtract(balance.multiply(cintr))).compareTo(BigDecimal.valueOf(0)) < 1) {
                payAr.add(balance);
            } else {
                payAr.add(payment);
            }

            intAr.add(balance.multiply(cintr));

            if (balance.subtract(payAr.get(i)).subtract(balance.multiply(cintr)).compareTo(BigDecimal.valueOf(0)) < 1) {
                prinAr.add(balance);
            } else {
                prinAr.add(payment.subtract(balance.multiply(cintr)));
            }

            payAr.set(i, payAr.get(i).round(new MathContext(2, RoundingMode.HALF_UP)));
            balAr.set(i, balAr.get(i).round(new MathContext(2, RoundingMode.HALF_UP)));
            intAr.set(i, intAr.get(i).round(new MathContext(2, RoundingMode.HALF_UP)));
            prinAr.set(i, prinAr.get(i).round(new MathContext(2, RoundingMode.HALF_UP)));
            payment = payAr.get(i).round(new MathContext(2, RoundingMode.HALF_UP));
            balance = balAr.get(i).round(new MathContext(2, RoundingMode.HALF_UP));
            System.out.println(prinAr);
            System.out.println(intAr);
        }

    }

In debug mode menu just goes blank and in regular mode it gives me divide by zero and references a prior part of my code which makes no sense because it isn't zero and stepping past it works fine. So what's the problem?

Aucun commentaire:

Enregistrer un commentaire