Split amount into inputs passed I have a requirement where I get totalAmount and how much parts to divide that amount into as inputs. For the outputresponse i am using builder pattern and also avoiding null fields. Total amount is a big decimal (if null or less than 0, throw exception) and number of inputs can range between 1-3 (if outside of this range or non numeric, throw exception). I have written this code, but I am not convinced and think there may be a cleaner and better way that is also easy to understand just by looking at the code. Also while splitting, whatever additional cents are left, that would be added to 1st amount.
eg. if 100.01 is total amount that i have to divide into 3 parts, i should get 33.35 for first amount and 33.33 for recurring amounts. Please advise if there is a cleaner and better way to achieve this.
public OutputResponse splitAmount(BigDecimal totalAmount, int divideInto) {
if (!(1 &lt;= divideInto &amp;&amp; 3 &gt;= divideInto)) {
            throw new Exception();
        }
        OutputResponse outputResponse;
        if (totalAmount != null &amp;&amp; totalAmount.compareTo(BigDecimal.ZERO) &gt; 0) {
            BigDecimal recurringAmounts = null;
            BigDecimal firstAmount = totalAmount;
            if (divideInto &gt; 1) {
                recurringAmounts = totalAmount.divide(BigDecimal.valueOf(divideInto), 2, RoundingMode.FLOOR);
                firstAmount = totalAmount.subtract(recurringAmounts.multiply(new BigDecimal(divideInto - 1)));
            }

            outputResponse = OutputResponse.builder()
                    .firstAmt(firstAmount)
                    .secondPmtAmt(recurringAmounts)
                    .build();

            if (divideInto &gt; 2) {
                outputResponse.setThirdPmtAmt(recurringAmounts);
            }
        } else {
            throw new Exception();
        }
}
```
Format
Use proper indentation, after a curly brace use a tab or 4 spaces.
Readability
Add a message with your exceptions. It's a terrible practice to just throw a regular exception and no message. You'd have to look at the line numbers in the stack trace in order to know where the exception message came from. It's better to have a user-friendly message describing the issue that occurred.
Do not mix ! with &gt; or &lt;. It's confusing to read.
Try to use negative or positive validation. For example, be consistent either checking if the inputs are wrong, or always check that their right. In other words have your error messages all at the top or bottom.
OutputResponse Is not a good name. It's meaningless. Assuming this is a class you made, consider changing the name. Also consider using an ArrayList rather than separate fields, that way you don't need to refactor to include a 4+ separations.
Example code:
public OutputResponse splitAmount(BigDecimal totalAmount, int divideInto) {
    if ((1 &gt; divideInto || 3 &lt; divideInto)) {
        throw new Exception(&quot;Divide into must be between 1-3&quot;);
    }
    
    if (totalAmount == null || totalAmount.compareTo(BigDecimal.ZERO &lt;= 0) {
        throw new Exception(&quot;Total amount must be a number greater than 0!&quot;);
    }
    
    BigDecimal recurringAmounts = null;
    BigDecimal firstAmount = totalAmount;
    if (divideInto &gt; 1) {
        recurringAmounts = totalAmount.divide(BigDecimal.valueOf(divideInto), 2, RoundingMode.FLOOR);
        firstAmount = totalAmount.subtract(recurringAmounts.multiply(new BigDecimal(divideInto - 1)));
    }

    OutputResponse outputResponse = OutputResponse.builder()
            .firstAmt(firstAmount)
            .secondPmtAmt(recurringAmounts)
            .build();

    if (divideInto &gt; 2) {
        outputResponse.setThirdPmtAmt(recurringAmounts);
    }
}