Thursday 15 August 2013

java - is there a way to validate data by saying "variable != int" -



java - is there a way to validate data by saying "variable != int" -

so want code say, avoid compile error's in event user selects "tea" instead of using corresponding integer.

if(appselection < 1 || appselection > appetizersarray.length || appselection != int)

the first 2 conditions grab info out of bounds, i'm looking way specify input needs int.

if alternative try catch i'm hoping there's more elegant solution.

you utilize regular expression. lower case \\d matches digits , adding plus means consecutive. so,

string regex = "\\d+"; string[] arr = { "tea", "123" }; (string str : arr) { if (str.matches(regex)) { system.out.printf("%s digits%n", str); } else { system.out.printf("%s not digits%n", str); } }

output is

tea not digits 123 digits

java if-statement int condition validation

No comments:

Post a Comment