Saidi Theory
Saidi Theory

Reputation: 19

not equals in java

I have this input " 4+4-(4+4+(4+4)))" in an array of strings "balance array"

I'm trying to execute this code:

String expression = "";
for(int j=2 ; j<balance.length-1 ; j++)
{
     if(!(balance[j].equals("+")) || !(balance[j].equals("-")) || !(balance[j].equals("(")) || !(balance[j].equals(")")))
          expression = expression + balance[j];
}

At the end of the code, expression is supposed to contain "444444", but it is not working.

Am I using the !.equals thing & the || thing in a wrong manner?

I want to combine those 4 statements together using || between them.

Upvotes: 0

Views: 467

Answers (2)

Manas Paldhe
Manas Paldhe

Reputation: 776

Use

if(!((balance[j].equals("+")) || (balance[j].equals("-")) || (balance[j].equals("(")) || (balance[j].equals(")"))))

and

for(int j=0 ; j<=balance.length-1 ; j++)

Upvotes: 0

NPE
NPE

Reputation: 500893

Change all the || to &&:

     if (!balance[j].equals("+") && !balance[j].equals("-") &&
         !balance[j].equals("(") && !balance[j].equals(")"))

Also, both the starting value of j and the loop's terminal condition look iffy.

Upvotes: 7

Related Questions