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

Time:10-25

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.

CodePudding user response:

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

  •  Tags:  
  • java
  • Related