简体   繁体   中英

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). 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]+"))) {

With this check, the number should be out of range and not a valid number. You should change this to OR || , ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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