AlexKondov

Java Sum Numbers From a Text File

May 18th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.47 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3.  
  4. public class SumNumbersFromATextFile {
  5.  
  6.     public static void main(String[] args) {
  7.         try {
  8.             BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
  9.             String line = null;
  10.             int sum = 0;
  11.             while ((line = reader.readLine()) != null) {
  12.                 sum += Integer.parseInt(line);
  13.             }
  14.             System.out.println(sum);
  15.         }
  16.         catch (Exception e) {
  17.                 System.out.println("Missing File");
  18.         }
  19.     }
  20.  
  21. }
Advertisement
Add Comment
Please, Sign In to add comment