0

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;
}
}
1
  • non-static method isNumeric(java.lang.String) cannot be referenced from a static context. Commented Apr 14, 2012 at 4:54

3 Answers 3

1

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];
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, my error is non-static method isNumeric(java.land.String) cannot be referenced from a static context. Unfortunately, I need to have my code run with JOptionPane and take in a number. I changed the regex to: String regex = "[1-8]"; I am still getting that error :/
Change the method isNumeric() to public static boolean isNumeric(String str) and try again. If it solves the problem, please tick that your question is solved and you have chosen an answer.
If your question is answered, please tick the correct answer so that everyone knows the problem is solved. Thanks
1

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

Comments

1

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.

1 Comment

Thanks, I have made that change but I am still getting a non-static method isNumerica(java.lang.String) cannot be referenced from a static context error. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.