casting to an integer


I am using an xml file as my datasource. One of my fields is a comma delimited list which I split and put into an array.... I have a variable assigned for each array element... so, $V{hr1} = $V{vends}[0]... $V{hr2} = $V{vends}[1]....

I want to take some of my variables and add them together.

$V{hr1} + $V{hr2} + $V{hr3}

My array is defined as a string. I've tried a couple of different options of defining my variables hr1, hr2, hr3.... as integers..... as well as I've tried them as string but nothing works to be able to add them together. I can get it to concatenate the values of hr1, hr2 and hr3 but I cannot get them to add.

I have a variable $V(hr_int4) where the expression is

Integer.valueOf($V{hr1} + $V{hr2} + $V{hr3})

when I view this I see the concatenated values of the fields as if they were strings.

I've tried

Integer.parseInt($V[hr1} + $V{hr2} + $V{hr3})

it didn't work..... I get an error "Incompatible conditional types int + Integer".

I've also gotten the error "The operator + is undefined for the argument type(s) java.lang.Integer, java.lang.Integer.

If anyone can tell me what I am missing doing, it would be greatly appreciated.

Thanks you in advance.
joannem's picture
Joined: Nov 12 2007 - 12:26am
Last seen: 15 years 6 months ago

1 Answer:

"+" is use to concatenate string values together so you cannot do

Integer.parseInt($V[hr1} + $V{hr2} + $V{hr3})

Parse each value independently then add them up

Integer.parseInt($V[hr1}) + Integer.parseInt($V{hr2}) + Integer.parseInt($V{hr3}))
svenn's picture
Joined: Mar 19 2007 - 5:57am
Last seen: 16 years 2 months ago