cancel
Showing results for 
Search instead for 
Did you mean: 

Java :Checking for Not equal to

Former Member
0 Kudos

Hi Group,

In Java How to Check the Condition for not equals to

When I am using the following code I am not getting expected result:



String str1 = new String();
String str2 = new String();
String str3 = new String();
String str4 = new String();

for (int i =0; i<a.length; i++)
{
str1=a<i>.substring(0,6);
str2=a<i>.substring(7,14); 
str3=a<i>.substring(18,23); 
str4=a<i>.substring(24,31); 
if ( (!str1.equals("000000")) || (!str2.equals("00000000")) || (!str3.equals("000000")) ||
    (!str4.equals("00000000")))
 
result.addValue("");

}

Accepted Solutions (0)

Answers (6)

Answers (6)

Former Member
0 Kudos

Hi,

Let us know the results.

kindly close the thread if ur problem is solved!

Regards,

Uma

Former Member
0 Kudos

or you can use this...this will also work

if ( !( (str1.equals("000000")) || (str2.equals("00000000")) || (str3.equals("000000")) ||

(str4.equals("00000000")) )

result.addValue("");

}

Note: I applied De Morgans law to what others said....[ !A && !B = !(A || B) ]

i am using something i learned during my bachelor's degree....

Former Member
0 Kudos

Hi,

The substring method in String class works as follows

substring(int beginIndex,int endIndex)

beginIndex - the beginning index, inclusive.

endIndex - the ending index, exclusive.

Here, str2 will have only 7 chars ( str2=a.substring(7,14); )

But u r comparing with 8 chars.

So always this condition will fail and not of this will return true.

Same for str3 and str4

So always the "if" will return "true".

This code will work properly

if ( (!str1.equals("000000")) || (!str2.equals("0000000")) || (!str3.equals("00000")) ||

(!str4.equals("0000000")))

This "if" will return "false" when str1, str2, str3 and str4 has all zeros

and return "true" if any one str has different value

Regards,

Uma

Former Member
0 Kudos

Hi Uma,

I need to check that If the Str1..str5 (all) each one not quals to '0000' then only proceed otherwise not

prabhu_s2
Active Contributor
0 Kudos

instead of having a number of if condition convert Str1..str5 to integer and sumup. check if greater than 0. else do ur operation.

Former Member
0 Kudos

Hi,

Then use the below code:

if (( !(str1.equals("000000")) && !(str2.equals("0000000")) && !(str3.equals("000000")) && !(str4.equals("00000000"))))

result.addValue("");

This may help you.

Thanks,

Rajeev Gupta

Former Member
0 Kudos

Hi,

change all OR (||) by AND (&&)

Regards,

Uma

Former Member
0 Kudos

After adding this below code I am getting the Error that


String str1 = new String();
String str2 = new String();
String str3 = new String();
String str4 = new String();
String str5 = new String();
String str6 = new String();
String str7 = new String();
String str8 = new String();
String str9 = new String();
String str10 = new String();
String str11 = new String();
String str12= new String();

for (int i =0; i<a.length; i++)
{
str1=a<i>.substring(0,5);
str2=a<i>.substring(6,13); 
str3=a<i>.substring(17,22); 
str4=a<i>.substring(23,30); 
if ( (!str1.equals("000000")) &&  (!str2.equals("00000000")) && (!str3.equals("000000")) &&
    (!str4.equals("00000000")))
//if ( (str1 != "000000")  || (str2 != "00000000") || (str3 != "000000") || (str4 != "00000000"))
result.addValue("");

}

for (int j =0; j<b.length; j++)
{
str5 =a[j].substring(0,5);
str6 =a[j].substring(6,13); 
str7=a[j].substring(17,22); 
str8 =a[j].substring(23,30); 
if ( (!str5.equals("000000")) &&  (!str6.equals("00000000")) &&  (!str7.equals("000000")) &&
    (!str8.equals("00000000")))
//if ( (str5 != "000000")  || (str6 != "00000000") || (str7 != "000000") || (str8 != "00000000"))
 result.addValue("");

// result.addValue("");
}
for (int k =0; k<c.length; k++)
{
str9 =a[k].substring(0,5);
str10=a[k].substring(6,13); 
str11=a[k].substring(17,22); 
str12=a[k].substring(23,30); 
if ( (!str9.equals("000000")) && (!str10.equals("00000000")) && (!str11.equals("000000")) &&
    (!str12.equals("00000000")))
//if ( (str9 != "000000")  || (str10 != "00000000") || (str11 != "000000") || (str12 != "00000000"))

 result.addValue("");

}

Sfter adding above Code I am getting this error

<b>

Exception:[java.lang.StringIndexOutOfBoundsException: String index out of range: 5] in class com.sap.xi.tf._MM_SR3_BACS_02_ method createTC$[, , ]

</b>

prabhu_s2
Active Contributor
0 Kudos

replace

for (int k =0; k<c.length; k++)

former_member187339
Active Contributor
0 Kudos

Hi,

>><i>if ( (!str1.equals("000000")) || (!str2.equals("00000000")) || (!str3.equals("000000")) ||

(!str4.equals("00000000")))

result.addValue("");</i>

Since u need

<i> I need to check that If the Str1..str5 (all) each one not quals to '0000' then only proceed otherwise not</i>

Either choose:

1) if ( (!str1.equals("0000")) && (!str2.equals("0000")) && (!str3.equals("0000")) &&

(!str4.equals("0000")))

result.addValue("");

or

2)

if ( (!str1.equals("000000"))

{

if (!str2.equals("00000000"))

{

if (!str3.equals("000000"))

{

if (!str4.equals("00000000")))

result.addValue("");

}

}

}

as you can see both servers the same method, But i would prefer to achieve the same functionality with the available node functions (like <b>if then else and substring</b>) in Message Mapping

Regards

Suraj

former_member187339
Active Contributor
0 Kudos

Hi swabap,

Dont declare these many variables:

<i>String str1 = new String();

String str2 = new String();

String str3 = new String();

String str4 = new String();

String str5 = new String();

String str6 = new String();

String str7 = new String();

String str8 = new String();

String str9 = new String();

String str10 = new String();

String str11 = new String();

String str12= new String();</i>

try to reuse str1, str2, str3, str4...

Check these:

1) str1=a<i>.substring(0,5);

should be str1=a<b><i></b>.substring(0,5);

2) for (int j =0; j><b.length; j++)

here i think you are taking the values from <b>b[]</b> array and check this

for (int j =0; j<b>><</b>b.length; j++)

try to execute one by one for loop then you will know in which you are getting error.

Regards

Suraj

Former Member
0 Kudos

Hi,

First check the length of a(i) that is it feasible to get its substring. Then do a substring of it like:

if (a(i).length() == 5)

str1= a(i).substring(0,5);

Do this thing for every str field.

You have an extra zero in all fields - this is wrong. While checking the condition in IF statement - put that many zeros according to length of str field. Like length of str1 is 5 so compare str1 with "00000". Do this thing for all str fields.

Then your UDF will run.

Thanks,

Rajeev Gupta

Message was edited by:

RAJEEV GUPTA

Former Member
0 Kudos

Hi,

if your req is that any of str1 to str4 is not equal to 0, then add in resultlist, then the below code may help you:

if (!( (str1.equals("000000")) && (str2.equals("00000000")) && (str3.equals("000000")) && (str4.equals("00000000"))))

result.addValue("");

Thanks,

Rajeev Gupta

prabhu_s2
Active Contributor
0 Kudos

if non of the strings to be not equal to zero then replace || (or) with && (And). Is this ur req?

Former Member
0 Kudos

Hi,

you will never get a result for your coding.

If you use NOT you can't use ||. You have to use &&.

(This is boolean algebra!)

Regards Mario