Extract integer from string...parseInt() / RegEx?

I’m having trouble extracting an integer from a string, and populating a Number item with it. I’m able to trim the string down to the digits I want, but for the life of me I can’t get it into an Integer object.

AzimuthString = "323 degrees 58 arc minutes 40 arc seconds  (NW)"
val Number AzimuthNumber = new Integer(transform("REGEX", "([0-9]+) .*", AzimuthString))

results in:

[ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule Solar Azimuth: An error occured during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.LogAction.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null

I’ve also tried Integer::parseInt(string) without success.

Thanks in advance for your help.

I remember having similar problems with Strings retrieved via the Exec plugin. In the end it was using something like

intvar = Integer::parseInt(stringvar.toString())

Go figure.

1 Like

Thanks, that solved it! For the record, here’s what finally worked:

val Number AzimuthValue = new String(transform("REGEX", "([0-9]+) .*", AzimuthString))
val Integer AzimuthNumber = Integer::parseInt(AzimuthValue.toString())
postUpdate(Azimuth, AzimuthNumber)

I’m not sure why I would need to convert a String into a String, but there we are.