I know eval is \"evil\", but I\'m using it in a way that the user can\'t ever abuse it.
Let\'s say I\'ve got a string \"new Integer(5)\". I want to do something suc
The Integer class takes a String in its constructor to set the value, assuming the provided string contains only numeric text.
Integer foo;
public void setFoo(String str) {
if(isInt(str)) {
foo = new Integer(str.trim());
}
}
// Returns a boolean based on if the provided string contains only numbers
private boolean isInt(String str) {
boolean isInt = true;
try {
Integer.parseInt(str.trim());
} catch (NumberFormatException nfe) {
isInt = false;
}
return isInt;
}
// Get the value as an int rather than Integer
public int getIntValue(String str) {
return Integer.parseInt(str.trim());
}