import java.util.Scanner; import java.io.*; public class fileread { public static void main (String[] args) throws IOException { String line; Scanner fileScan, lineScan; int sum = 0; int x = 0; // Could easily take the filename as input or from command line fileScan = new Scanner (new File("foo.txt")); // Read and process each line of the file while (fileScan.hasNext()) { line = fileScan.nextLine(); lineScan = new Scanner (line); // want to scan this line while (lineScan.hasNext()) { // Throws an exception if the next token is not an int!! // Should really catch that exception ;) x = lineScan.nextInt(); sum = sum + x; } System.out.println(sum); } } }