title: Java: Program is not counting occurrences of element inside arraylist
body: <p>I am making a program that will count the occurrences of &quot;red&quot; and &quot;blue&quot; cars when user inputs a string of the colours of the cars passing by on one line.</p>
  <p>I have tried to implement an arraylist and counter but it does not seem to be working. I would just like to to know what am I doing wrong?</p>
  <p>Code:</p>
  <pre><code>package com.company;
  
  import java.util.Scanner;
  import java.util.ArrayList;
  import java.util.*;
  
  public class Main {
    public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
  
       String red = &quot;red&quot;;
       String blue = &quot;blue&quot;;
       int red_count =  0;
       int blue_count =  0;
       System.out.println(&quot;Cars: &quot;);
       String x = scanner.nextLine();
  
       ArrayList&lt;String&gt; arr = new ArrayList&lt;String&gt;();
       arr.add(x);
  
       for (int i = 0; i &lt; arr.size(); i++) {
          String c = arr.get(i);
          if (c.equals(&quot;red&quot;)) {
              red_count = red_count + 1;
          }
          if (c.equals(&quot;blue&quot;)) {
              blue_count = blue_count + 1;
          }
          System.out.println(&quot;red: &quot; + red_count);
          System.out.println(&quot;blue: &quot; + blue_count);
       }
      }
     }
  </code></pre>
  <p><strong>Input:</strong>
  red green blue yellow</p>
  <p><strong>Output:</strong></p>
  <p>red: 0</p>
  <p>blue: 0</p>