-2
 String [] P = K.split(" ");
//NB: the value of K is "malaria zika AIDS"

 for (int x=0;x< P.length; x++) 
   {
       if (P[x]=="zika") 
          { 
            System.out.println( "This is zika virus P[x]="+ P[x]); 
               }else{
           System.out.println( "This is NOT zika virus P[x]="+P[x]); 
          }
   }

Expecting

This is NOT zika virus P[x]=Malaria
This is zika virus P[x]=zika
This is NOT zika virus P[x]=AIDS

But Getting

This is NOT zika virus P[x]=Malaria
This is NOT zika virus P[x]=zika
This is NOT zika virus P[x]=AIDS

What am I missing? I believe that this is the part with the problem. if (P[x]=="zika")

  • compare string with `equals()` method – Nambi Feb 06 '16 at 08:48
  • To test if objects are equals you must use `equals` method, this `P[x].equals("zika")`. You are testing if references are the same, which is not the same, if your bank account is filled with 100$ as mine, you will agree that they are not same accounts (`!=`), but their debt is the same (`equals`). – Jean-Baptiste Yunès Feb 06 '16 at 08:50
  • thanks. Makes sense. Working now. – user5891396 Feb 06 '16 at 08:55

1 Answers1

0

change this line

if (P[x]=="zika")  to if(P[x].equals("zika") )
KVK
  • 1,257
  • 2
  • 17
  • 48