// Displays the first 5 numbers in the given file, // and displays their sum at the end. import java.io.*; // file import java.util.*; // scanner public class FileSum2 { public static void main(String[] args) throws FileNotFoundException { Scanner fileInput = new Scanner(new File("numbers.txt")); double sum = 0; while (fileInput.hasNext()) { if (fileInput.hasNextDouble()) { // read a number, add it to sum double number = fileInput.nextDouble(); System.out.println("number = " + number); sum += number; } else { // skip it String goAway = fileInput.next(); System.out.println("skipping " + goAway); } } System.out.println("Sum = " + sum); } }