title: Incorrect total of sum calculation
body: <p>I'm trying to solve this question:</p>
  
  <blockquote>
    <p>Write a Java program that reads a filename from the user. The file is expected to contain a maximum of 20 integers.
    Declare an array with the size 20. Read all the values from the file and store them in the array. Take note that there
    can be any number of integers in the file. Lastly, calculate and display the sum of all integers stored in the array.
    Use exception handling to detect:</p>
    
    <ul>
    <li>Improper inputs from the file where a non-integer is read</li>
    <li>Using of invalid array index</li>
    <li>Invalid filename where he file does not exist</li>
    </ul>
  </blockquote>
  
  <p>My problem currently is the incorrect sum. Here is my code      </p>
  
  <pre><code>package labtask.pkg10;
  import java.io.File;
  import java.io.*;
  import java.util.*;
  import java.util.ArrayList;
  
  public class task2 {
     public static void main(String[] args) {
         int integers[] = new int[20];
         Scanner read = new Scanner(System.in);
         int sum = 0;
         int num = 0;
         String filename;
         System.out.println("enter the file name ");
         filename = read.next();
  
         try {
             File file = new File(filename);
             Scanner inputFile = null;
             inputFile = new Scanner(file);
  
             int i = 0;
             while (inputFile.hasNext()) {
                 num = Integer.parseInt(inputFile.next());
  
                 integers[i] = num;
             }
  
             for (int x = 0; x &lt; 20; x++) {
                 sum += num;
             }
             System.out.println("sum are : " + sum);
         } catch (FileNotFoundException e) {
             System.out.println("file not found");
         } catch (NumberFormatException e) {
             System.out.println("please enter only integer number");
         }
     }
  }
  </code></pre>
  
  <p>and my text file:</p>
  
  <pre><code>2
  2
  2
  2
  2
  2
  2
  2
  2
  12
  23
  2
  2
  2
  2
  2
  2
  2
  2
  2
  </code></pre>
  
  <p>I get the following output:</p>
  
  <blockquote>
    <p>enter the file name<br>
    gg.txt<br>
    sum are : 40<br>
    BUILD SUCCESSFUL (total time: 2 seconds)</p>
  </blockquote>
  
  <p>Why isn't the sum counted correctly?</p>