# Clean Coding (Refactoring Repetitive Patterns):

**URL:** <https://forum.codewithmosh.com/t/clean-coding-refactoring-repetitive-patterns/7896>\
**Category:** Java\
**Created:** [October 12, 2021, 11:15am UTC](https://forum.codewithmosh.com/t/clean-coding-refactoring-repetitive-patterns/7896 "2021-10-12T11:15:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bchuru](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@bchuru](https://forum.codewithmosh.com/u/bchuru)\
**Post date:** [October 12, 2021, 11:15am UTC](https://forum.codewithmosh.com/t/clean-coding-refactoring-repetitive-patterns/7896/1 "2021-10-12T11:15:07Z")

</div>

Whenever I use decimals for my inputs i.e the Annual Interest Rate,  
I get “Exception in thread” Error:  
Before refactoring I did not have this issue;  
Does anyone know why this is happening?

```auto
int principle= (int) readNumber("Principle :", 1_000 , 1_000_000 );
       float annualInterest= (float) readNumber("Annual Interest Rate :", 1 , 30 );
       byte years= (byte) readNumber("Period (Years) : ", 1 , 30 );

        double mortgage = calculateMortgage(principle, annualInterest, years);

        String mortgageFormatted = NumberFormat.getCurrencyInstance().format(mortgage);
        System.out.println("Mortgage: " + mortgageFormatted);
    }

    public static double readNumber (String prompt, double min, double max) {
        Scanner scanner = new Scanner(System.in);
        double value;
        while (true) {
            System.out.print(prompt);
            value = scanner.nextInt();
            if (value >= min && value <=max)
                break ;
            System.out.println("Please Enter a value between " + min + "and " + max );
        }
        return value;
    }

    public static double calculateMortgage(
            int principle,
            float annualInterest,
            byte years) {

        final byte MONTHS_IN_YEAR = 12;
        final byte Percent = 100;

        float monthlyInterest = annualInterest / Percent / MONTHS_IN_YEAR;
        short numberOfPayments = (short)(MONTHS_IN_YEAR * years);
        double mortgage = principle * (monthlyInterest * Math.pow( 1 + monthlyInterest, numberOfPayments )
                / (Math.pow( 1 + monthlyInterest, numberOfPayments) -1
        ));
        return mortgage;
    }

```

---

<div class="post-metadata">

**Author:** ![SAM](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/sam/32/167_2.png) [@SAM](https://forum.codewithmosh.com/u/SAM)\
**Post date:** [October 12, 2021, 4:09pm UTC](https://forum.codewithmosh.com/t/clean-coding-refactoring-repetitive-patterns/7896/2 "2021-10-12T16:09:53Z")

</div>

You are asking the Scanner for the next **Int** which expects an integer as an input.

---

<div class="post-metadata">

**Author:** ![Gintsm](https://avatars.discourse-cdn.com/v4/letter/g/f05b48/32.png) [@Gintsm](https://forum.codewithmosh.com/u/Gintsm)\
**Post date:** [October 16, 2021, 9:01am UTC](https://forum.codewithmosh.com/t/clean-coding-refactoring-repetitive-patterns/7896/3 "2021-10-16T09:01:12Z")

</div>

Not a Scanner - But for me seems - in line, where you initialize mortgageFormated as a string but trying to format number. in that case, you should convert to string Or my suggestion ```  
mortgageFormatted

```auto

```

---

<div class="post-metadata">

**Author:** ![Gintsm](https://avatars.discourse-cdn.com/v4/letter/g/f05b48/32.png) [@Gintsm](https://forum.codewithmosh.com/u/Gintsm)\
**Post date:** [October 16, 2021, 9:03am UTC](https://forum.codewithmosh.com/t/clean-coding-refactoring-repetitive-patterns/7896/4 "2021-10-16T09:03:07Z")

</div>

suddenly lost part of my comment 🙂 use double for variable mortgageFormatted
