title: How to solve exception in thread "main", java.lang.ArithmeticException: / by zero?
body: <p>I keep getting an exception in thread "main":</p>
  
  <pre><code>java.lang.ArithmeticException: / by zero
  at PersonalityTest.percentage(PersonalityTest.java:85)
  at PersonalityTest.main(PersonalityTest.java:19)
  </code></pre>
  
  <p>And I want to add the count every time the scanner reads A or B and get the percentage of B.</p>
  
  <pre><code>import java.io.*;
  import java.util.*;
  
  public class PersonalityTest {
  
      public static final int dimen = 4;
  
      public static void main(String [] args) throws FileNotFoundException {
          Scanner input = new Scanner(new File("personality.txt"));
          PrintStream out = new PrintStream(new File("output.txt"));
  
          int[] a = new int[4];
          int[] b = new int[4];
  
          welcome();
  
          while (input.hasNextLine()) {
             String letter = letter(input, out);
             countNum(a, b, out, letter);
             int[] percentage = percentage(a, b, out);
             type(out, percentage);
             out.println("");
          }
      }
  
      public static void welcome () {
          System.out.println("The Keirsey Temperament Sorter is a test that measures four independent dimensions of your personality: ");
          System.out.println("1. Extrovert versus Introvert (E vs. I): what energizes you");
          System.out.println("2. Sensation versus iNtuition (S vs. N): what you focus on");
          System.out.println("3. Thinking versus Feeling (T vs. F): how you interpret what you focus on");
          System.out.println("4. Judging versus Perceiving (J vs. P): how you approach life");
          System.out.println("");
      }
  
  
      public static String letter (Scanner input, PrintStream out) {
          String name = input.nextLine();
          out.println(name + ":");
          String letter = input.nextLine();
          return letter;
      }
  
      public static void countNum(int[] a, int[] b, PrintStream out, String letter) {
          int[] countA = new int[7];
          int[] countB = new int[7];
          int n = 0;
  
          out.print("answers: [");
  
          for (int i = 0; i &lt; letter.length(); i++) {
              int type = i % 7;
  
              if (letter.charAt(i) == 'A' || letter.charAt(i) == 'a') {
                 n = 1;
              }
              else if (letter.charAt(i) == 'B' || letter.charAt(i) == 'b') {
                 n = 1;
              }
              countA[type] += n;
              countB[type] += n;
  
              if (type == 2 || type == 4 || type == 6) {
                 a[type / 2] = countA[type - 1]+ countA[type];
                 b[type / 2] = countB[type - 1]+ countA[type];
              } else if (type == 0) {
                 a[0] = countA[0];
                 b[0] = countB[0];
              }
              for (int j = 0; j &lt; dimen; j++) {
                  out.print(a[j] + "A-" + b[j] + "B," + " ");
              }
          }
          out.print("]");
      }
  
      public static int[] percentage (int[] a, int[] b, PrintStream out) {
         int[] percentage = new int [4];
         out.print("percent B: [");
         double n = 0.0;
  
         for (int i = 0; i &lt; dimen; i++) {
             n = b[i] * 100 / (a[i] + b[i]); // &lt;--- This is where I get error
             percentage [i] = (int) Math.round(n);
             out.print(percentage[i]);
         }
  
         out.print("]");
         return percentage;
      }
  
      public static void type (PrintStream out, int[] percentage) {
          String[] type = new String [4];
  
          out.print("type: ");
  
          for (int i = 0; i &lt;= dimen; i++) {
              while (percentage[i] &gt; 50) {
                  if (i == 0) {
                      type[1] = "I";
                  }
                  else if (i == 1) {
                      type[2] = "N";
                  }
                  else if (i == 2) {
                      type[3] = "F";
                  }
                  else {
                      type[4] = "P";
                  }
              }
  
              while (percentage[i] &lt; 50) {
                  if (i == 0) {
                      type[1] = "E";
                  }
                  else if (i == 1) {
                      type[2] = "S";
                  }
                  else if (i == 2) {
                      type[3] = "T";
                  }
                  else {
                      type[4] = "J";
                  }
              }
              out.print(Arrays.toString(type));
          }
      }
  }
  </code></pre>