cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Groovy CPI: String to Integer

anirban_pwc
Participant
0 Likes
6,246

Hi Experts -

I'm trying to increment a local variable value by 1, but when I read the header value, it maps to a String "value", then I'm trying to cast it to an Integer with the statement:

counter_var = Integer.parseInt(value)

but receiving a Java exception:

java.lang.Exception: java.lang.NumberFormatException: For input string

Please help me resolve this issue.

Thank you,

Anirban

import com.sap.gateway.ip.core.customdev.util.Message
import java.util.HashMap
def Message processData(Message message) {
       //Headers 
       def map = message.getHeaders()
       def value = map.get("INCREMENT")
       if(value == null)
       { 
           def counter_var = 0       
       }
       else 
       {
           counter_var = Integer.parseInt(value)
       }
       
       message.setHeader("INCREMENT", counter_var + 1)
       return message;
}
View Entire Topic
sunny_kapoor
Product and Topic Expert
Product and Topic Expert

Hi amallick,

Script looks fine just move the counter_var declaration outside.

If you still see the error just check the value of INCREMENT header in the Trace mode, may be it is not having the proper number in String fromat.

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;

def Message processData(Message message) {
       def map = message.getHeaders()
       def value = map.get("INCREMENT");
       def counter_var;
       if(value == null)
       { 
         counter_var = 0       
       }
       else 
       {
           counter_var = Integer.parseInt(value)
       }
       
       message.setHeader("INCREMENT", counter_var + 1)
       return message;
}