Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I print an error message if the value is < 250

I have this code where somebody can choose a ppm value and it calculates the fanControl value.


static int fanControl(int ppm) {
      
    if (ppm < 401 && ppm > 250 ) {
        return 0;   
    } else if (ppm >= 401 && ppm < 800) {
        return (ppm - 400)/4;
    } else if (ppm >= 800) {
        return 100;
    } else {
        return;
    }
}

What do I have to do that the program prints an error message and stops calculating if the person who chooses the ppm value, chooses something less than 250.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I agree with @Federico

Your solution will depend on how you want to handle invalid input

Just change return; to return -1;

Then, whenever/wherever you call fanControl(X), just surround it with a conditional:

if (fanControl(X) == -1){
   throw new IllegalArgumentException("ppm value must be at least 250");
} else {
   //Do whatever you were planning to do
}

Also, make sure your ppm > 250 is changed to ppm >= 250 if you want to include 250 as valid input

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading