1

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;

    }
}
3
  • You should define a boolean function to validate integer and use it from the GUI. Commented Jan 29, 2017 at 8:36
  • 2
    Welcome to Stack Overflow! It looks like you need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please feel free to come back with more details. Commented Jan 29, 2017 at 8:40
  • @JoeC Thanks I'll check it out! Commented Jan 29, 2017 at 9:04

3 Answers 3

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

Comments

1

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

should be

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

Comments

1

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 ||, i.e. 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.

Comments

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.