仅使用计数器对数字范围和数字值进行JAVA输入验证

时间:2014-04-24 19:03:34

标签: java validation counter

我正在尝试完成一项任务,我不确定要采取的路线。我尝试了whileif和语句组合,无法获得我需要的输入验证。

  • 我正在尝试验证用户输入并确保其输入是0到10之间的数字(不包括0和10)。
  • 此外,我需要确保他们输入的内容是数字,而不是某些符号或字母。
  • 最后我需要一个计数器,让他们有3次机会输入正确的信息。

以下代码是我尝试设置的方法来完成此任务。

private static int getNumericInput(String quantity) {
    int count = 0;
    String input;
    input = JOptionPane.showInputDialog(quantity);
    int range = Integer.parseInt(input);

    while ((range > 9 || range < 1) && (count < 2)) {
        JOptionPane.showMessageDialog(null, 
                "Sorry that input is not valid, please choose a quantity from 1-9");
        input = JOptionPane.showInputDialog(quantity);
        count++;
    }
    if (count == 2) {
        JOptionPane.showMessageDialog(null, 
                "Sorry you failed to input a valid response, terminating.");
        System.exit(0);
    }
    return range;
}

4 个答案:

答案 0 :(得分:1)

正如其他人所说,看看String是否是有效整数,你会发现NumberFormatException

try {
    int number = Integer.parseInt(input);
    // no exception thrown, that means its a valid Integer
} catch(NumberFormatException e) {
    // invalid Integer
}

但是我还要指出一个代码更改,这是一个do while循环的完美示例。当你想要使用循环但在第一次迭代结束时运行条件时,while循环是否很好。

在您的情况下,您始终希望获取用户输入。通过在第一个循环之后评估while循环条件,您可以减少循环之前必须执行的一些重复代码。请考虑以下代码更改。

int count = 0;
String input;
int range;
do {
    input = JOptionPane.showInputDialog(quantity);
    try {
        range = Integer.parseInt(input);
    } catch(NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Sorry that input is not valid, please choose a quantity from 1-9");
        count++;
        // set the range outside the range so we go through the loop again.
        range = -1;
    }
} while((range > 9 || range < 1) && (count < 2));

if (count == 2) {
    JOptionPane.showMessageDialog(null, 
            "Sorry you failed to input a valid response, terminating.");
    System.exit(0);
}
return range;

答案 1 :(得分:0)

这一行:

int range = Integer.parseInt(input);
在你的while循环之前出现

。所以,你知道如何将输入转换为int。下一步是要意识到每次用户给你输入时你都应该这样做。你快到了。

答案 2 :(得分:0)

如果输入字符串在字符串中不包含有效数字格式,则此行将抛出NumberFormatException

int range = Integer.parseInt(input)

你需要把它试试catch块

try {
            Integer.parseInt("test");
        } catch (java.lang.NumberFormatException e) {
                count++; //allow user for next attempt.
              //    showInputDialog HERE
              if(count==3) {
                // show your msg here in JDIalog.
              System.exit(0);
           }
}

有3次机会输入正确的信息,你需要使用循环,内部循环调用你的showInputDialog方法

答案 3 :(得分:0)

从JDK 1.8开始,您可以使用 temporal 包解决此问题,并使用try-catch来解决此线程的其余答案:

import java.time.temporal.ValueRange;
...
public static void main (String[] args){
ValueRange range = ValueRange.of(1,9);

    int num, counter = 0;

    do {
    System.out.print("Enter num: ");
    num = Integer.parseInt(scanner.next());
    if (range.isValidIntValue(num))
        System.out.println("InRange");
    else 
        System.out.println("OutOfRange");

    counter ++;
    } while (!range.isValidIntValue(num) && counter < 3);
...
}
相关问题