#How do I immediately throw exception when even just 1 input is invalid in a series of inputs?

1 messages · Page 1 of 1 (latest)

mild bridge
#

I have written a program to calculate interest amount.

It takes:

  • interest rate
  • year
  • loan amount

And calculates the interest to be paid.

I've enclosed this in try-catch blocks

try{
getInterest;
getYear;
getLoan;
Make a new loan class's object based on these values.
}

Say I entered:

-5
-5
-5

I only get the first invalid input's exception message after entering all three inputs. However, I want to avoid it. Is there a way to do so?

humble nicheBOT
#

<@&987246399047479336> please have a look, thanks.

humble nicheBOT
unreal plank
#

please indent your code. it improves the readablity. + 4 spaces (1 tab) in the lines under a { and after the } you go back to the indentation level from before the { this is standard and makes it much easier to read code.

faint spindle
#

What's holding you back from immediately validating it in your second class if that's your wish?

unreal plank
#
  • you should get one line from stdin so getLine() and then do validation checks on the line do not read directly a double etc from stdin
  • if you want to print an error message directly after one input you need to do validation after every input
unreal plank
unreal plank
faint spindle
young ledge
#

Instead of throwing an exception for each input, use a StringBuilder and append each exception message that pops up. Then, throw one big exception with the concatenated exception messages.

faint spindle
young ledge
mild bridge
#

can you hint me towards what should I research for "keyword" I mean exact ones. Just studied about excpetion handling chapter from d.liang's java...

faint spindle
#

Well that was your question.

#

And drop that course and do:

humble nicheBOT
#

For learning Java, we recommend MOOC.

It is a completely free introductory Java course created by the University of Helsinki, it is a great way to learn Java from the ground up.

Visit MOOC here:
https://java-programming.mooc.fi
(the course is available in both English and Finnish)

  • The MOOC teaches a broad introduction to programming in Java in two parts - one at beginner, and another at intermediate level.
    The end of the course is marked by creating your own Asteroids game clone!
  • The MOOC allows using features up to Java 11 - you can install Temurin OpenJDK 11 from the Adoptium project.
  • To submit exercises for evaluation, you need to configure an Editor/IDE (Integrated Development Environment) with the TMC Plugin.

The course instructions will suggest to use TMCBeans/NetBeans or VS Code for the course, but you can also use IntelliJ, which we generally recommend.

  • TMCBeans/NetBeans is the easiest to configure - but has the most dated user experience
  • VS Code is very popular as an editor, but it is quite new for Java Development. Some extra configuration is needed.
  • IntelliJ arguably has the best user experience and is most widely used Java IDE by professionals.
    IntelliJ requires installing a version no newer than 2023.1 - because the IntelliJ TMC Plugin doesn't work with newer installs.
    The IntelliJ Community version is completely free and all you need to install the TMC plugin.

To use IntelliJ with the MOOC, simply install the TMC plugin by opening IntelliJ -> File -> Settings -> Plugins and searching for TMC. You will then be able to use IntelliJ to complete MOOC.

About the course - Java Programming

mild bridge
#

wil l do.however still unsure what should be solution

#

don't give me solution but guide me towards it

mild bridge
#

public class LoanTest {
public static void main(String[] args) {
testLoan(5.0, 10, 5000); // Valid input
testLoan(0, 10, 5000); // Invalid interest rate
testLoan(5.0, 0, 5000); // Invalid number of years
testLoan(5.0, 10, -1000); // Invalid loan amount
}

private static void testLoan(double rate, int years, double amount) {
    try {
        Loan loan = new Loan(rate, years, amount);
        System.out.println("Loan created successfully!");
        System.out.println("Annual Interest Rate: " + loan.getAnnualInterestRate());
        System.out.println("Number of Years: " + loan.getNumberOfYears());
        System.out.println("Loan Amount: " + loan.getLoanAmount());
        System.out.println("Monthly Payment: " + loan.getMonthlyPayment());
        System.out.println("Total Payment: " + loan.getTotalPayment());
        System.out.println("-----------------------------------");
    } catch (IllegalArgumentException e) {
        System.out.println("Error: " + e.getMessage());
    }
}

}

just wrote a quick solution

#

close this

#

@close

#

#close

faint spindle
#

It's /help-thread close @mild bridge .