简体   繁体   English

Java:验证输入是否为一定数字

[英]Java: validating if input is a certain number

A driver class, inputoutput class and numberValidator class. 驱动程序类,inputoutput类和numberValidator类。 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. 用户将使用j选项窗格弹出框输入数字1-8,如果输入的不是1-8,则应该显示一条错误消息。 If the number is 1-8 the rest of the code (not written here) is supposed to continue to run. 如果数字为1-8,则其余代码(此处未编写)应继续运行。 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;
}
}

Probably you meant: String regex = "[1-8]\\." 可能是您的意思是: String regex = "[1-8]\\." - ie a single digit in interval 1-8 followed by a dot? -即间隔1-8中的一位数字后跟一个点?

You can simplify your regex to String regex = "[1-8]"; 您可以将您的正则表达式简化为String regex = "[1-8]"; .

This means that your regex can only accept digit from 1 to 8. 这意味着您的正则表达式只能接受1到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. 您从未初始化过numberValidator因此编译器看到要在没有numberValidator实例的情况下访问isNumeric方法,并看到方法isNumeric没有关键字static。 That's why it's tells you the message error. 这就是为什么它告诉您消息错误的原因。

Changin your if statement like this correct your problem : 像这样更改您的if语句可以纠正您的问题:

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

or make your method isNumeric() static. 或将您的方法isNumeric()静态。

BTW : a class name must have the first character in capitals. 顺便说一句:类别名称必须以大写字母开头。

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. 在下面的代码中,我添加了一个else ,如果选择的值不是数字,它将重新运行displayMenu()方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM