import java.io.FileReader; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.InputMismatchException; public class Sum { public static void main(String args[]) { FileReader reader; try { reader = new FileReader("data.txt"); } catch (FileNotFoundException e) { System.out.println("The file could not be opened."); return; } // make a scanner from it Scanner in = new Scanner(reader); // read all the numbers int sum = 0; while (in.hasNext()) { try { sum += in.nextInt(); } catch (InputMismatchException e) { // read the word and ignore it in.next(); } } // print the sum System.out.println("Sum is " + sum + "."); } }