A driver class, inputoutput class and numberValidator class. The user is to input a number 1-8 using a j-option pane popup box, if what is entered is not 1-8 an error message is supposed to display. If the number is 1-8 the rest of the code (not written here) is supposed to continue to run. I’m getting errors, anyone see where I’m wrong?
///Driver class (excludes package)////
public class Driver {
public static void main(String[] args) {
InputOutput inputoutput = new InputOutput();
inputoutput.displayMenu();
}
}
///InputOutput class (excludes package and import for JOptionPane)///
public class InputOutput {
public int displayMenu()
{
String stringChoice = JOptionPane.showInputDialog("Restaurant:\n"
+ "1. Pancakes: $10.00\n"
+ "2. Bananas: $1.00\n"
+ "3. Bread: $2.00\n");
if (numberValidator.isNumeric(stringChoice)){
choiceNumber = Integer.parseInt(stringChoice);
}
return choiceNumber;
}
///numberValidator class code (excludes package)///
public class numberValidator {
public boolean isNumeric(String str)
{
boolean valid = true;
String regex = "[1-8/.]*";
valid = str.matches(regex);
return valid;
}
}
What is the error you are getting? Is it simply that it continues running no matter what the user chooses? In my code below, I have added an
elsethat will re-run thedisplayMenu()method if the chosen value was not a number.But for your problem, wouldn’t it be better to use a dropdown list of options?…