title: Why else statement is executing although if statement is true?
body: <pre><code>import java.util.Scanner;
  
  class candidate {
  
      public String name;
      public int count;
  
      public candidate(String name) {
          super();
          this.name = name;
      }
  
  }
  
  public class DayScholar {
      public static void main(String[] args) {
  
          Scanner in = new Scanner(System.in);
          candidate[] candidates = new candidate[3];
          candidates[0] = new candidate(&quot;vikas&quot;);
          candidates[1] = new candidate(&quot;ganesh&quot;);
          candidates[2] = new candidate(&quot;teja&quot;);
  
          System.out.print(&quot;No. of voters : &quot;);
          int voters = in.nextInt();
          in.nextLine();
          for (int i = 0; i &lt; voters; i++) {
              System.out.print(&quot;vote : &quot;);
              String name = in.nextLine().toLowerCase();
              for (int j = 0; j &lt; 3; j++) {
  </code></pre>
  <p>Here is the code, although if the statement is true else is also executing. How to check the condition</p>
  <pre><code>                if (name.equals(candidates[j].name)) {
                      candidates[j].count++;
                  } else {            **//problem here**
                      System.out.println(&quot;N&quot;);
                      break;
                  }
  
  
              }
          }
  
          int highest = 0;
          String winner = &quot;&quot;;
          for (int i = 0; i &lt; 3; i++) {
              if (candidates[i].count &gt; highest) {
                  highest = candidates[i].count;
                  winner = candidates[i].name;
              } else if (candidates[i].count == highest) {
                  winner += (&quot;\n&quot; + candidates[i].name);
              }
          }
  
          System.out.println(winner);
      }
  }
  </code></pre>