jeudi 25 janvier 2018

Java - if statement provides incorrect answer when doing multiple tests

The code below is used to provide the year that a car registration plate was made. For example, if registration = "RD56 FRD" then the first IF statement is used and the else statement is used when lastTwoDigitsOfYear is < 50. The code below does work but I have ran into a problem during testing. Whilst the else statement is able to calculate the year from registration "ER05 RAS" (giving back 2005), the year returns 2006 when the registration is "PF46 LSH" when it should be returning 2046 for the year and I can't see where I've gone wrong. Any advice would be greatly appreciated.

int year = 2000;
        String thirdAndFourthCharsOfReg = registration.substring(3,4);
        int lastTwoDigitsOfYear = Integer.parseInt(thirdAndFourthCharsOfReg);
        int correctCarRegDigits = lastTwoDigitsOfYear - 50;
        if (lastTwoDigitsOfYear >= 50) {
            year = year + correctCarRegDigits;
            return year;
        } else {
            year = year + lastTwoDigitsOfYear;
            return year;
        }

Aucun commentaire:

Enregistrer un commentaire