Interesting way to validate number

Today when I read my co-worker's codes, I found an interesting way to validate if a string is numeric.

Here is my way to do.  Because I'm not good at regex, every time I need to search online and find a solution.

public static boolean isNumeric(String s) {  
	    return java.util.regex.Pattern.matches("\\d+", s);  
}

Here is my co-worker's code. It's really smart, right? I have never thought I can do in this way.

 protected boolean isNumber(String fieldValue){
		boolean isValid = true; 
	    try{
			Integer field = Integer.parseInt(fieldValue);
			if(field <= 0){
				isValid = false;
		    }
		}catch(NumberFormatException e){
			isValid = false;
		}
		return isValid;
			
}

你可能感兴趣的:(java,java,java,java,numeric)