user1332839
user1332839

Reputation: 3

Java: validating if input is a certain number

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;
}
}

Upvotes: 0

Views: 186

Answers (3)

user973999
user973999

Reputation:

You can simplify your regex to String regex = "[1-8]";.

This means that your regex can only accept digit from 1 to 8.

Regards.

Edit :

For your error :

you never initialized numberValidator so the compilator see that you want to acces the isNumeric method without an instance of numberValidator and see that the medthod isNumeric has not the keyword static. That's why it's tells you the message error.

Changin your if statement like this correct your problem :

if ( new numberValidator().isNumeric(stringChoice))

or make your method isNumeric() static.

BTW : a class name must have the first character in capitals.

Upvotes: 1

wattostudios
wattostudios

Reputation: 8764

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 else that will re-run the displayMenu() method if the chosen value was not a number.

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);   
        }
        else {
            return displayMenu();
            }
        return choiceNumber;
    }

But for your problem, wouldn't it be better to use a dropdown list of options?...

String[] options = new String[]{"Pancakes: $10.00","Bananas: $1.00","Bread: $2.00"}

int chosenIndex = JOptionPane.showOptionDialog(null, "Choose a menu item", "Menu", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);

String chosenValue = options[chosenIndex];

Upvotes: 1

Eugene Retunsky
Eugene Retunsky

Reputation: 13139

Probably you meant: String regex = "[1-8]\." - i.e. a single digit in interval 1-8 followed by a dot?

Upvotes: 1

Related Questions