How can I verify if a string contains at least one number?
F Federico Desia Newbie Feb 19, 2019 24 2 Apr 1, 2019 #1 Apr 1, 2019 Add bookmark #1 How can I verify if a string contains at least one number?
D Deleted User Guest Apr 2, 2019 #2 Apr 2, 2019 Add bookmark #2 You could do it with a regex Code: String line = "te5t"; Pattern r = Pattern.compile(".*[0-9].*"); Matcher m = r.matcher(line); if (m.find( )) { System.out.println("It matched"); } Reactions: GameTheory, scary alien, mikedt and 1 other person Upvote 0
You could do it with a regex Code: String line = "te5t"; Pattern r = Pattern.compile(".*[0-9].*"); Matcher m = r.matcher(line); if (m.find( )) { System.out.println("It matched"); }
scary alien not really so scary VIP Member Mar 5, 2010 22,305 23,781 Indy Apr 2, 2019 #3 Apr 2, 2019 Add bookmark #3 +1 to LV I've also used this method before: public static boolean isNumeric(String str) { try { Double.parseDouble(str); return true; } catch(NumberFormatException e){ return false; } } (taken from the excellent site: https://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-numeric-in-java that also has a lot more/other suggestions on how to accomplish this) Cheers! Reactions: GameTheory and Deleted User Upvote 0
+1 to LV I've also used this method before: public static boolean isNumeric(String str) { try { Double.parseDouble(str); return true; } catch(NumberFormatException e){ return false; } } (taken from the excellent site: https://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-numeric-in-java that also has a lot more/other suggestions on how to accomplish this) Cheers!
D Deleted User Guest Apr 2, 2019 #4 Apr 2, 2019 Add bookmark #4 scary alien said: +1 to LV Click to expand... Thanks Scary. I have to give 0.5 of that to @GameTheory who put the idea of regex in my head with an answer to a recent question. Reactions: GameTheory and scary alien Upvote 0
scary alien said: +1 to LV Click to expand... Thanks Scary. I have to give 0.5 of that to @GameTheory who put the idea of regex in my head with an answer to a recent question.