cancel
Showing results for 
Search instead for 
Did you mean: 

Code to count number of string appearances and return a nunber of counts

Former Member
0 Kudos

Hi All

I am trying to count the number of appearance of my the string when so my UDF code is:

public String StringCounter(String BM_CH_ESS,Container container)

{

String searchFor = "BM_CH_ESS";

String value = null;

int searchLength = searchFor.length();

int Count = 0;

int index = 0;

for( int i = 0; i < value.length(); i++ )

{

index = value.indexOf(searchFor, i);

if ( index != -1 )

{

Count++;

i += index + searchLength;

}

}

return Count;

}

When ever press F7 I get the error below:

Basical I want to return a number of counts.

Source code has syntax error: C:/usr/sap/###/DVEBMGS00/j2ee/cluster/server0/./temp/classpath_resolver/Map6dc873d0e26211df88c6000c2973cb67/source/com/sap/xi/tf/_MM_MDM_MATERIAL_TO_CATALOGUE_.java:500: incompatible types found : int required: java.lang.String return Count;

Can u please give some suggestions

Thanks

Yonela

Accepted Solutions (1)

Accepted Solutions (1)

stefan_grube
Active Contributor
0 Kudos

> public String StringCounter(String BM_CH_ESS,Container container)

> int Count = 0;

> return Count;

You have to return a String, not an int.

Test your Java code with an external tool first:

/people/stefan.grube/blog/2005/12/30/test-user-defined-functions-for-the-xi-graphical-mapping-tool-in-developer-studio

Former Member
0 Kudos

As mentioned above, you will need to use return statement as below:

return Integer.toString(count);

Answers (1)

Answers (1)

justin_santhanam
Active Contributor
0 Kudos

Yonela,

Why you are using for loop here? What's your actual requirement?

Former Member
0 Kudos

Hi ALL

Thanks for ur assistance, I managed to find the solution I went to my NetBeans aditor to write it:

I wanted to write a function like this to count the string appearance"

This is my below code:

public class Main {

/**

  • @param args the command line arguments

*/

public static void main(String[] args)

{

String inputString = "C:C:C";

String searchFor = "C";

int leng = searchFor.length();

int count = 0;

if(leng > 0)

{

int Index = inputString.indexOf(searchFor);

while(Index != -1)

{

count++;

Index = inputString.indexOf(searchFor, Index + leng );

}

}

System.out.println("Count = " + count);

}

}

Thanks inadvance

Yonela