cancel
Showing results for 
Search instead for 
Did you mean: 

udf to count the number of occurances

Former Member
0 Kudos

Hello All

I am trying to write an UDF which can count the number of occurances of charecter ' in a string and fives the result as number of occurances - 3 . For example if the char ' is present 20 times in a string , I want the result to be 17.

Could you help me in creating this UDF please, I am a beginner to Java.

Thank you

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

Try this code,

int count = 0;

String str;

for(int i=0; i=a.strlength();i++)

{

if (a<i> = 'a')

{

count = count + 1;

}

}

count = count - 3;

str = integer.toString(count);

return str;

Regards,

Sreeni

Former Member
0 Kudos

Hi Sreeni ,

I tried you code , but it has some compilation errors ,

The cod ei used is

//write your code here

int count = 0;

String str;

for(int i=0; i=input.strlength();i++)

{

if (input = '\'')

{

count = count + 1;

}

}

count = count - 3;

str = integer.toString(count);

return str;

Just as you suggested ,

for it I got these errors

incompatible types

found : int required: boolean

for(int i=0; i=input.str.length();i++)

cannot resolve symbol

symbol : variable str

location: class java.lang.String

for(int i=0; i=input.str.length();i++)

incompatible types

found : java.lang.String

required: boolean if (input = '\'')

cannot resolve symbol

symbol : variable integer

location: class com.sap.xi.tf._MMPOFileToPayload_

str = integer.toString(count);

cheers

justin_santhanam
Active Contributor
0 Kudos

Sudeer,

Please go ahead with Aasish code, it will work for sure.

raj.

Former Member
0 Kudos

Hi Raj

I tried Aasish's code too , It too had syntax errors ,

they are

unclosed character literal

String search ='"";

unclosed string literal

String search ='"";

How can i ret rid of these errors

Thank you

Former Member
0 Kudos

The error i get with Aashish's code is

operator == cannot be applied to char,java.lang.String

if (input.charAt(i) == "a")

cannot resolve symbol

symbol : variable integer

location: class com.sap.xi.tf._MMPOFileToPayload_

value = integer.toString(res);

Any ideas ?

aashish_sinha
Active Contributor
0 Kudos

Hi,

Just Change double quotes with single quotes..

like this

if (input.charAt(i) == 'a')

Use this code

String search ="";

search = "a";

int count = 0;

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

{

if (input.charAt(i) == 'a')

count = count + 1;

}

int res = count - 3;

count = 0;

String value = "";

value = integer.toString(res);

return value;

Regards

Aashish Sinha

Edited by: Aashish Sinha on Oct 31, 2008 1:12 PM

Former Member
0 Kudos

Hi Aashish

That erro is gone afte ri changed to ' ' but there is still one more error which is

cannot resolve symbol

symbol : variable integer location: class com.sap.xi.tf._MMPOFileToPayload_

value = integer.toString(res);

Former Member
0 Kudos

>

> cannot resolve symbol

> symbol : variable integer

> location: class com.sap.xi.tf._MMPOFileToPayload_

> value = integer.toString(res);

>

> Any ideas ?

Hi,

For the above, you'll need to change it as:

value = String.valueOf(res);

Regards,

Alka.

aashish_sinha
Active Contributor
0 Kudos

Hi.

Now use this code like this

String value = " " + res;

return value;

Hi alka,

Here we need to convert Int to String not to string to int. Also in above code just use capital I instead of small i, it will start working.

Regards

Aashish Sinha

Edited by: Aashish Sinha on Oct 31, 2008 1:19 PM

Former Member
0 Kudos

Hi Aashish,

Kindly take some time to go thru the following [Java String API|http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html].

Regards,

Alka.

Former Member
0 Kudos

Hi Guys

As Suggested by Alka , when i replaced

value = String.valueOf(res);

It did work , also I agree what Aashish is saying . will try Ashish's enhancement too .

aashish_sinha
Active Contributor
0 Kudos

Thanks !!

Answers (2)

Answers (2)

prasad_ulagappan2
Contributor
0 Kudos

Use this code,

public String tst(String a,Container container){

//write your code here

int count = 0;

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

{

if (String.valueOf(a.charAt(i)).equals("a"))

count = count +1;

}

count = count -3;

return Integer.toString(count);

}

Here I have checked for the occurance of character 'a' if u need any other character change it accordingly...

This will work for sure...

aashish_sinha
Active Contributor
0 Kudos

Hi,

Lets say input string is input = "aashish sinha"

String search ='"";

search = "a";

int count = 0;

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

{

if (input.charAt(i) == "a")

count = count + 1;

}

int res = count - 3;

count = 0;

String value = "";

value = integer.toString(res);

return value;

Regards

Aashish Sinha

Edited by: Aashish Sinha on Oct 31, 2008 12:15 PM