Search code examples
javaarraysequalsequality

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.


Solution

  • Use

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

    and

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