Java method stuck in endless loop while validating input -
In some way I'm getting some fun games here and I can not see why now
this method It is believed that the input is either y or n and is not empty, I should note that this is for school, and 2 separate error output is necessary.
When I enter an empty row, I get a blank line on the console. After that point, or after a time where I intentionally enter the wrong data, like X, the next time I enter a valid data such as y or n, then I continue to get bad data in the endless loop .
What have I done?
Get the Public Stable Boolean Policy (Scanner Scan) {Boolean Decision = False; System.out.println ("continue? Y / n:"); Letter userChoice = sc.next (); Boolean isValid = false; While (isValid == is incorrect) {if (userChoice.equalsIgnoreCase ("y")) {decision = true; IsValid = true; } And if (userChoice.equalsIgnoreCase ("n")) {decision = false; IsValid = true; } And if (userChoice.equals ("")) {System.out.println ("error! This entry is required, try again."); UserChoice = sc.next (); } Else if (! UserChoice.equalsIgnoreCase ("y") | (! UserChoice.equalsIgnoreCase ("n") {System.out.println ("Error! Entry should be 'Y' or 'N'); UserChoice = SC.next ();}} Return Decision; Note: The code was modified to include
New Console Output (still incorrect)
Formatted Results Loan Amount: $ 5.00 Annual Interest Rate: 500% Number of years: 5 monthly payments: $ 2.08
Continue? Y / N:
B error! Login 'Y' or 'N' should try again Y error! Should be 'Y' or 'N', try again Y Enter the loan amount:
Do not set variable userChoice for new user. If last
System.out.println ("Error! Entry should be 'Y' or 'N', try again.) ; UserChoice = sc.next ();
Comments
Post a Comment