Question

I am new to JRules coding.It may looks like a simple question. I would like to check the length of an number in JRules code. How to write the code for that. "the length of" phrase is expecting a string. I tried to use toString() to convert the number to String. But this is not working. Please give some idea.

Was it helpful?

Solution

You can do that in multiple different ways, quite like going to Rome. :)

1/ In your Java:

public static int getIntLength(final int myLovelyNumber) {
    return (new Integer(myLovelyNumber)).toString().length();
}

Then, you add this lovely function to your BOM, and you verbalise it. Personally, I would create a separate library for such Helpers. An update will do the trick.

2/ In your BOM:

I will assume that you start from scratch
a/ Create a 'virtual' object in your BOM (personally, I would create a separate BOM for 'virtual' objects) and set the "execution name" (or something like that) to java.lang.object
b/ create a 'virtual' static method in the the 'virtual' object with one parameter: Integer returning a: int
c/ in the B2X of your new 'virtual' method add:
return param.toString().length();
d/ verbalise the method

Of course it is up to you to check for null value in Java or B2X.

These are the main ways of doing it. One could find more tricky ways, but for a beginner (or not), it is a good approach. Hope it helps.

PS: If you are happy with that then mark the answer as correct :)

OTHER TIPS

What about negatives?, your solution will count the '-' as a significant value.

So part (1) should be...

public static int getIntLength(final int myLovelyNumber) {
    return (Integer.valueOf(Math.abs(myLovelyNumber))).toString().length();
}

Also an 'int' can't be null. When you come to test it it'll be zero.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top