title: Why doesn't this If statement properly compare the stored variable with an element in an array?
body: <p>So i'm just doing some elementary testing while I learn java, and i'm curious why this doesn't work:</p>
  <pre><code>public static void main(String[] args) {
          String[] names={&quot;John&quot;,&quot;Mark&quot;,&quot;Suzy&quot;,&quot;Anthony&quot;};
          String[] passwords={&quot;passw0rd&quot;,&quot;CandyC4ne&quot;,&quot;x5021&quot;,&quot;ADawg55&quot;};
          System.out.println(&quot;Please indicate which user you would like to log in as:\n(1)John\n(2)Mark\n(3)Suzy\n(4)Anthony&quot;);
          Scanner x=new Scanner(System.in);
          int y=x.nextInt()-1;
          System.out.print(&quot;Please enter the password to log into &quot;+names[y]+&quot;'s account: &quot;);
          Scanner a=new Scanner(System.in);
          String newpass=a.nextLine();
          System.out.println(passwords[y]);
          if(passwords[y]==newpass){
              System.out.print(&quot;Hello &quot;+names[y]+&quot;!&quot;);
          }else{
              System.out.println(&quot;Incorrect Password. &quot;+passwords[y]+&quot; &quot;+newpass);
          }
      }
  </code></pre>
  <p>Both passwords[y] and newpass are the exact same string, and when I try:</p>
  <p><code>if(passwords[y]==&quot;passw0rd&quot;);</code></p>
  <p>(And obviously pick the 'John') it does work, so i'm hoping someone can explain the difference and how to get around it so as to better accept user input.</p>
  <p>Also i'll take a for loop as an answer, but i'm hoping I can avoid it.</p>