I’m feeling silly that I cannot figure this out, but it’s really starting to piss me off.
I’m simply trying to ensure the string contains only digits using string.match(regex). If it contains any non-numeric characters, hard code it to 9999999.
Here’s my code. I’m essentially checking to see if the results I pull from the ResultSet moduleResults contains no non-numeric characters before use it to setEndPointID which accepts a long as its parameter. The trim() is in there because there are often leading spaces in id_amr_module and I don’t want those to throw off the regex match. I’ve also tried the regex [0-9]* with no success.
String strEndPointID = moduleResults.getString("id_amr_module");
strEndPointID.trim();
if(strEndPointID.matches("\\d*")){
msiRF.setEndpointID(moduleResults.getLong("id_amr_module"));
}
else{
long lngEndPointID = 99999999;
msiRF.setEndpointID(lngEndPointID);
}
You need start and end anchors to make sure the entire string is numeric.You also need to use+instead of*so that the regexp matches at least 1 digit (^\\d*$would match the empty string). Fully refactored: