简体   繁体   English

字符串到整数输入验证转换

[英]String to Integer Input Verification Conversion

I'm writing a class to verify my input from another class. 我正在编写一个类来验证我从另一个类中输入的内容。 Everything seems to work except that my verification won't accept the exact high range value. 一切似乎都正常,除了我的验证不会接受确切的高范围值。 I have no idea why it won't accept it as -- for example -- if the highRange is 10 and the user inputs 10, !(10>10). 我不知道为什么它不接受它-例如-如果highRange是10并且用户输入10,!(10> 10)。 If you guys could care to my code over I would appreciate it! 如果你们能照顾我的代码,我将不胜感激!

import javax.swing.JOptionPane;

public class InputVerification {
private String input;
private int lowRange;
private int highRange;
private int invalidNum;

public InputVerification(String input, int lowRange, int highRange, int invalidNum) {
    this.input = input;
    this.lowRange = lowRange;
    this.highRange = highRange;
    this.invalidNum = invalidNum;
}

public int intVerify() {

    String userInput = null;
    int intInput = 0;
    do {
        do {
            try {

                /**
                 * handles any text entered instead of numbers enter -2 if
                 * you don't need an invalid number
                 */

                userInput = JOptionPane.showInputDialog(this.input);

                intInput = Integer.parseInt(userInput);

                if ((intInput > highRange || intInput < lowRange) && !(userInput.matches("[a-zA-Z]+"))) {

                    JOptionPane.showMessageDialog(null,
                            "Error! Please pick a number between " + lowRange + "-" + highRange + ".");

                }

            } catch (NumberFormatException e) {

                    JOptionPane.showMessageDialog(null,
                            "Error! Please pick a number between " + lowRange + "-" + highRange + ".");

            }

        } while (!userInput.matches("^[0-9]"));

        if ((intInput > highRange || intInput < lowRange)) {

            /**
             * 
             * sends an error message if the number is higher than 100 or
             * lower than 1 as long as the input was not text
             * 
             */

            JOptionPane.showMessageDialog(null,
                    "Error! Please pick a number between " + lowRange + "-" + highRange + ".");

        }

        if (invalidNum != -2 && intInput == invalidNum) {
            JOptionPane.showMessageDialog(null, "Error! Please pick a number between " + lowRange + "-" + highRange
                    + " that is not " + invalidNum + ".");
        }

    } while ((intInput > highRange || intInput < lowRange || intInput == invalidNum)
            && !(userInput.matches("[a-zA-Z]+")));

    return intInput;

    }
}
import javax.swing.JOptionPane;

public class InputVerification {

   public static Integer parseInt( String value, int min, int max ) {
      try {
         final int iValue = Integer.parseInt( value );
         if(( min <= iValue ) && ( iValue <= max )) {
            return iValue;
         }
      }
      catch( final Throwable t ) {/**/}
      return null;
   }

   public static int getInteger( String message, int lowRange, int highRange ) {
      Integer intValue = null;
      do {
         final String userInput = JOptionPane.showInputDialog( message );
         intValue = parseInt( userInput, lowRange, highRange );
         if( intValue == null ) {
            JOptionPane.showMessageDialog(null,
               "Error! Please pick a number in [" +
               lowRange + ".." + highRange + "]" );
         }
      } while( intValue == null );
      return intValue.intValue();
   }

   public static void main( String[] args ) {
      getInteger("Hello!", 0, 10 );
   }
}

while (!userInput.matches("^[0-9]"));

应该

while (!userInput.matches("^[0-9]+"));

There is an error in your if condition for checking whether the number is in range: if ((intInput > highRange || intInput < lowRange) && !(userInput.matches("[a-zA-Z]+"))) { 您的if条件是否存在错误,无法检查数字是否在范围内: if ((intInput > highRange || intInput < lowRange) && !(userInput.matches("[a-zA-Z]+"))) {

With this check, the number should be out of range and not a valid number. 进行此检查后,该数字应超出范围, 并且不是有效数字。 You should change this to OR || 您应该将其更改为OR || , ie if ((intInput > highRange || intInput < lowRange) || !(userInput.matches("[a-zA-Z]+"))) { ,即if ((intInput > highRange || intInput < lowRange) || !(userInput.matches("[a-zA-Z]+"))) {

Please note that the check if the input matches your regular expression (if it is a number) is not going to be very useful, since you would get an exception when parsing it as an integer earlier on. 请注意,检查输入是否与您的正则表达式(如果是数字)匹配不是很有用,因为在更早地将其解析为整数时会出现异常。

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

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