Advertisement
Guest User

8. Sum Numbers from a Text File

a guest
May 21st, 2014
119
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.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class _08_SumNumbersFromTextFile {
  7.  
  8.     public static void main(String[] args) {
  9.         File file = new File("Input.txt");
  10.         long sum = 0;
  11.         try {
  12.             Scanner sc = new Scanner(file);
  13.             while (sc.hasNext()) {
  14.                 sum += Integer.parseInt(sc.nextLine());
  15.             }
  16.         } catch (FileNotFoundException e) {
  17.             System.out.println("Error");
  18.             return;
  19.         }
  20.         System.out.println(sum);
  21.     }
  22.  
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement