title: Why do I have an off by one error?
body: <p>In the below code, I am simply trying to count the number of times an item appears in a file. However, when I print out the keys and their values I get a count that is one more that there actually is. When I initialize <code>total</code> to 0 it fixes the problem but I am not sure why.</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.DataInputStream;
  import java.io.FileInputStream;
  import java.io.InputStreamReader;
  import java.util.HashMap;
  import java.util.HashSet;
  
  public class Problem {
     public static void main(String[] arg) {
        HashSet QID = new HashSet();
        HashMap QIDToCorrect = new HashMap();
        try {
           // Open the file that is the first command line parameter
           FileInputStream fstream = new FileInputStream(
                 "C:/Users/lol/Downloads/data.csv");
  
           // Get the object of DataInputStream
           DataInputStream in = new DataInputStream(fstream);
           BufferedReader br = new BufferedReader(new InputStreamReader(in));
  
           //Read File Line By Line
           String strLine;
           br.readLine(); //skip header line
           int total = 0;
           int blah = 0;
           while ((strLine = br.readLine()) != null) {
  
              String[] split = strLine.split(",");
  
              if (!QID.contains(split[0])) {
                 total = 1;
                 QID.add(split[0]);
                 QIDToCorrect.put(split[0], total);
              } else {
                 total += 1;
                 QIDToCorrect.put(split[0], total);
              }
  
              //System.out.println();
           }
        } catch (Exception e) {
        }
     }
  }
  </code></pre>