I am adding a feature to an existing module to automate combat. That feature prompts the attacker to enter the Attack Factor and Defense Factor of the combat as a string which is expected to be in the format of “AF:DF” where AF and DF are each supposed to be a decimal number between 0 and 99. For example:
9:5
12:2
The logic in my module currently parses the input string into its two components, separated by the colon character, and verifies that each of those two substrings have a length of 1 or 2. I would like to add error checking to verify that each of those two substrings represent a valid decimal number, by checking that it consists only of the characters “0” through “9”.
I was hoping to find a BeanShell function that could do that, but did not find one in the Vassal reference manual “Expressions” section. I tried to use a regular expression (in the Property Match section of a Trigger trait), but was unsuccessful.
The best method that I can think of to do that syntax checking is to note the starting length of the substring (either 1 or 2), then add 0 to it (substring + 0) and check the length of the resulting value. I’m guessing that BeanShell will try to interpret that substring as a number and if it is, then adding 0 should result in the same number with the same length when treated as a string. This seems very cumbersome however.
Might there be a better way to test whether a string is a valid decimal number?
Cheers,
Jim Hunter.