<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: Groovy CPI: String to Integer in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/groovy-cpi-string-to-integer/qaa-p/12546574#M4703945</link>
    <description>&lt;P&gt;Thank you,  &lt;SPAN class="mention-scrubbed"&gt;sunny.kapoor2&lt;/SPAN&gt; and  @ganesh m for your kind responses.&lt;/P&gt;&lt;P&gt;This is the script that finally worked for me:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;

def Message processData(Message message) {
       def map = message.getHeaders()
       String value = map.get("INCREMENT");
       def counter_var;
       if(value == null || value == "")
       { 
         counter_var = 0       
       }
       else 
       {
		  counter_var = Integer.parseInt(value)
       }
       
       message.setHeader("INCREMENT", counter_var + 1)
       return message;
}&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 10 Nov 2021 20:52:08 GMT</pubDate>
    <dc:creator>anirban_pwc</dc:creator>
    <dc:date>2021-11-10T20:52:08Z</dc:date>
    <item>
      <title>Groovy CPI: String to Integer</title>
      <link>https://community.sap.com/t5/technology-q-a/groovy-cpi-string-to-integer/qaq-p/12546571</link>
      <description>&lt;P&gt;Hi Experts -&lt;/P&gt;
  &lt;P&gt;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:&lt;/P&gt;
  &lt;P&gt;counter_var = Integer.parseInt(value)&lt;/P&gt;
  &lt;P&gt;but receiving a Java exception:&lt;/P&gt;
  &lt;P&gt;java.lang.Exception: java.lang.NumberFormatException: For input string&lt;/P&gt;
  &lt;P&gt;Please help me resolve this issue.&lt;/P&gt;
  &lt;P&gt;Thank you,&lt;/P&gt;
  &lt;P&gt;Anirban&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;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;
}&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 Nov 2021 18:06:15 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/groovy-cpi-string-to-integer/qaq-p/12546571</guid>
      <dc:creator>anirban_pwc</dc:creator>
      <dc:date>2021-11-10T18:06:15Z</dc:date>
    </item>
    <item>
      <title>Re: Groovy CPI: String to Integer</title>
      <link>https://community.sap.com/t5/technology-q-a/groovy-cpi-string-to-integer/qaa-p/12546572#M4703943</link>
      <description>&lt;P&gt;Hi &lt;SPAN class="mention-scrubbed"&gt;amallick&lt;/SPAN&gt;,&lt;/P&gt;&lt;P&gt;Script looks fine just move the counter_var declaration outside.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;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;
}

&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 Nov 2021 18:25:16 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/groovy-cpi-string-to-integer/qaa-p/12546572#M4703943</guid>
      <dc:creator>sunny_kapoor</dc:creator>
      <dc:date>2021-11-10T18:25:16Z</dc:date>
    </item>
    <item>
      <title>Re: Groovy CPI: String to Integer</title>
      <link>https://community.sap.com/t5/technology-q-a/groovy-cpi-string-to-integer/qaa-p/12546573#M4703944</link>
      <description>&lt;P&gt;Hi &lt;SPAN class="mention-scrubbed"&gt;amallick&lt;/SPAN&gt;,&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;As &lt;SPAN class="mention-scrubbed"&gt;sunny.kapoor2&lt;/SPAN&gt; mentioned Declaration of counter_var should be outside. And &lt;/LI&gt;&lt;LI&gt;If condition should verify empty string condition as well. - &lt;STRONG&gt;if(value == null || value == "")&lt;/STRONG&gt;&lt;/LI&gt;&lt;LI&gt;It's also better to use an additional condition &lt;STRONG&gt;value.isNumber() == true&lt;/STRONG&gt; and handle the case before casting the string to integer. Below Nested condition is just an example. You can make changes and optimize the code as required.&lt;/LI&gt;&lt;/OL&gt;&lt;PRE&gt;&lt;CODE&gt;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 || value == "")
       { 
         counter_var = 0       
       }
       else 
       {
          if(value.isNumber() == false)
		{
		  //make the counter to zero / raise custom exception / take any other action
		}
	    else
		counter_var = Integer.parseInt(value)
       }
       
       message.setHeader("INCREMENT", counter_var + 1)
       return message;
}

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 Nov 2021 20:23:49 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/groovy-cpi-string-to-integer/qaa-p/12546573#M4703944</guid>
      <dc:creator>former_member683114</dc:creator>
      <dc:date>2021-11-10T20:23:49Z</dc:date>
    </item>
    <item>
      <title>Re: Groovy CPI: String to Integer</title>
      <link>https://community.sap.com/t5/technology-q-a/groovy-cpi-string-to-integer/qaa-p/12546574#M4703945</link>
      <description>&lt;P&gt;Thank you,  &lt;SPAN class="mention-scrubbed"&gt;sunny.kapoor2&lt;/SPAN&gt; and  @ganesh m for your kind responses.&lt;/P&gt;&lt;P&gt;This is the script that finally worked for me:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;

def Message processData(Message message) {
       def map = message.getHeaders()
       String value = map.get("INCREMENT");
       def counter_var;
       if(value == null || value == "")
       { 
         counter_var = 0       
       }
       else 
       {
		  counter_var = Integer.parseInt(value)
       }
       
       message.setHeader("INCREMENT", counter_var + 1)
       return message;
}&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 Nov 2021 20:52:08 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/groovy-cpi-string-to-integer/qaa-p/12546574#M4703945</guid>
      <dc:creator>anirban_pwc</dc:creator>
      <dc:date>2021-11-10T20:52:08Z</dc:date>
    </item>
  </channel>
</rss>

