2

I am trying to develop an UDF (All Values of Queue) which takes a string as input contains pipe (|) delimiter for both fields and field values. Also, string contains both Header and Detail.

Header fields will come once and Detail in 'N' number of times in a single line.

Below is the input string :

Headerfield1|fieldValue1|Headerfield2|fieldValue2|Headerfield3|fieldValue3|Headerfield4|fieldValue4|Headerfield5|fieldValue5|Headerfield6|fieldValue6|Itemfield1|fieldvalue1|Itemfield1|fieldvalue1|Itemfield2|fieldvalue2|Itemfield3|fieldvalue3|Itemfield4|fieldvalue4|Itemfield1-1|fieldvalue1-1|Itemfield2-1|fieldvalue1-2|Itemfield3-1|fieldvalue3-1|Itemfield4-1|fieldvalue4-1|Itemfield1-2|fieldvalue1-2|Itemfield2-2|fieldvalue2-2|Itemfield3-2|fieldvalue3-2|Itemfield4-2|fieldvalue4-2|

Say in above string, Headerfield1 is a field and its corresponding value is fieldValue1.

In UDF, we are passing input string in variable input and required field in the variable field.

I am trying to pass the field name and expecting its corresponding field value as output.

Below is the UDF:

public void StringSplit(String[] input, String[] field, ResultList result, Container container) throws StreamTransformationException{

String str = input.toString();
String tokens[] = str.split("\\|");
Map<String, String>tokensMap = new HashMap();
  int j =0;
  for(int i =0; i <tokens.length ; i++)
  {
    if(j == i)
    {
     String key = tokens[i].toString();
       if (key.contains("-"))
     {
     String key2 = key.substring(0, key.indexOf("-"));
     if(tokensMap.containsKey(key2))
     {
     tokensMap.put(key2, tokensMap.get(key2)+","+tokens[i+1].toString());
     }
     else
     {
     tokensMap.put(key.substring(0, key.indexOf("-")),tokens[i+1].toString());
     }
     }
     else
     {
     tokensMap.put(tokens[i].toString(),tokens[i+1].toString());
     }
     j = i+2;
    }
   }
  String[] result1 = tokensMap.get(field).split(",");
  for (int i = 0; i < result1.length; i++)
{
if (result1[i].equals("") || result1[i].equals(null) )
result.addSuppress();
else
      result.addValue(result1[i]);  
}
}

This UDF, takes input string and splits with pipe (|) and kept it in tokens later add it to tokensMap. Finally get the required field and stores into the result1 and by using for loop adding into the result list. I am getting the error below:

Exception:[java.lang.ArrayIndexOutOfBoundsException:] in class com.sap.xi.tf.

Method StringSplit

Please check the error and suggest where I need to change.

Thanks in advance

Thank you, Chakradhar N!

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
chakradhar
  • 21
  • 2
  • 2
    `tokens[i+1]` is very dangerous.. Debug your code to understand why. – Maroun Oct 20 '14 at 07:15
  • 1
    Please take more time to format your code - the indentation makes it very hard to read. Additionally, you should try to obtain a full stack trace, with the relevant line number. `tokens[i + 1]` does indeed look like the most likely culprit here, but you should get into better diagnostic practices early. – Jon Skeet Oct 20 '14 at 07:20
  • Does this answer your question? [How can I avoid ArrayIndexOutOfBoundsException or IndexOutOfBoundsException?](https://stackoverflow.com/questions/32568261/how-can-i-avoid-arrayindexoutofboundsexception-or-indexoutofboundsexception) – OldProgrammer Jun 18 '23 at 19:12

0 Answers0