I am doing a validation. I get two strings back, one of them has to be a numeric value less than 10000 and the other string has to have a non-empty String value.
I propose the following:
public boolean validar(String val1, String val2) {
if(!esNum(gpag) && Integer.parseInt(gpag) > 10000 && q.isEmpty()) {
return false;
}else {
return true;
}
}
public static boolean esNum(String cadena){
try{
if(cadena!= null){
Integer.parseInt(cadena);
}
}catch(NumberFormatException nfe){
}
return false;
}
Could someone tell me if it is correct or not?
You have some flaws in logic:
false
.Integer.parseInt
twice, unnecessarily.You could do something like:
Apart from what @pablo-lozano provided, a small additional suggestion.
As long as you have this structure in your code
You can simplify it as
If we join this recommendation with Pablo's proposal, the validate method would be