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