Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Enter mortgage term in years (1, 2, 3, 5, 10): 7
7 is an invalid term, please re-enter.
Enter mortgage term (1, 2, 3, 5, 10): 1
Enter mortgage amortization period (5, 10, 15, 20, 25): 30
30 is an invalid amortization period
Enter mortgage amortization period (5, 10, 15, 20, 25): 25

What I have tried:

Java
if (mortageTerm == 4 || mortageTerm == 6 || mortageTerm == 7 || mortageTerm == 8 ||  mortageTerm == 9 ){
                System.out.print(mortageTerm + " is an invalid term, please re-enter.
}
Posted
Updated 27-Jun-19 18:55pm
v2
Comments
[no name] 27-Jun-19 16:57pm    
Keep trying. When you actually have "something", then you'll get some feedback.
wseng 27-Jun-19 23:07pm    
use do-while loop

1 solution

Instead of listing "invalid" values, usea a switch[^] statement to split out the values, and use default to print your error:
Java
boolean valid = false;
do {
   ... prompt and read your value ...
   switch (value) {
      case 1:
      case 2:
      case 3:
      case 5:
      case 10:
         valid = true;
         break;
      default:
         ... show your error message ...
      }
   } while (!valid);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900