cancel
Showing results for 
Search instead for 
Did you mean: 

UDF: How to convert string with sign into integer

Former Member
0 Kudos

Hi

I have a number e.g 123456 (xsd:string) and sign - or + (xsd:string) and both are concatenated and fed into UDF. I want this string to be converted into Integer. If I use the below kind of statement it is not working and throwing the below error.

int BilledAmount = Integer.parseInt(b[0]);

Exception:

RuntimeException in Message-Mapping transformation: Exception:[java.lang.NumberFormatException: For input string: "+000000000006684"]

Any guess on this issue ??

Regards

Kumar

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

use

int BilledAmount = Integer.parseInt(b);

Thanx

Aamir

Former Member
0 Kudos

Aamir,

I am using the context rather to value. So your suggestion won't work for me

Thanks

kumar

justin_santhanam
Active Contributor
0 Kudos

Kumar,

The problem is , you can't convert the String to Integer if it has <b>+</b> symbol in front of it. If you have negate symbol it will work.

Follow something like below example.

http://www.flickr.com/photo_zoom.gne?id=2089259274&size=o

http://www.flickr.com/photo_zoom.gne?id=2089259270&size=o

http://www.flickr.com/photo_zoom.gne?id=2089259266&size=o

raj.

VijayKonam
Active Contributor
0 Kudos

Do not append + sign. Remove leading zeros. Then append only if the sign is -. Use Integer.parseInt(b[0]). I believe since you are caching the context, at 0 level you would have the entire string.

VJ

VijayKonam
Active Contributor
0 Kudos

Or simply convert to Integer them multiply with -1 when the sign is -.

VJ

Former Member
0 Kudos

Hey buddy

>>If you have negate symbol it will work.

do you mean that if he has -000000000006684 instead of +000000000006684 then Integer.parseInt(b[0]) would work?

my thinking is that in either case b[0] would return either + or - and will throw a NaN exception.

i m not a Java Guru so i might be wrong:)

Thanx

Aamir

justin_santhanam
Active Contributor
0 Kudos

Hey buddy,

Hope you are doing gr8, I'm getting ready leaving this friday..:-)

Aamir b[0] doesn't correspond to the first character of the string. He has multiple input values, hence he is using Context. See my structure and UDF I mentioned above, actually the above is his requirement I believe.

Yes, If you have + symbol in front of String and if you try to conver to Integer object it will throw an exception. Coz it doesn't make sense to add + symbol at all. if you don't have any sign in front it implies it's positive, am I right? But in case of negative, you need to explicitly include - symbol in front.

This is the reason that java couldn't able to convert the string that has + symbol in front, but it can in case it has - symbol in front.

I hope it clears a bit..

raj.

Former Member
0 Kudos

I m doing great,enjoy your trip,i myself am leaving on christmas eve:)

>>b[0] doesn't correspond to the first character of the string. He has multiple input values, hence he is using Context

yeah,i didn't notice he is using context.

too bad i cant open flick here:( will check it out once i get back home

Have fun on your trip

Thanx

Aamir

Answers (2)

Answers (2)

Former Member
0 Kudos

>>"+000000000006684"

at 0th position you can have a + or a - sign,then why do you think parseInt(b[0]) will work?

it will definitely throw an execption coz at 0th position you have a sign(+ or -) and not any number.

what exactly is your requirement anyways?

Thanx

Aamir

Message was edited by:

Aamir Suhail

Former Member
0 Kudos

try removing the leading zeros.

regards

Mustafa