/* CSE 142, Autumn 2007, Marty Stepp This program reads a file and adds all its numeric tokens. This version skips over any non-numeric tokens. */ import java.io.*; // for File import java.util.*; // for Scanner public class Echo { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("numbers.txt")); double sum = 0.0; while (input.hasNext()) { if (input.hasNextDouble()) { // next token is a number; read and add it to sum double token = input.nextDouble(); System.out.println("number = " + token); sum += token; } else { // junk token (not a number); throw it away String throwaway = input.next(); // System.out.println("I'm skipping this token: " + throwaway); } } System.out.println("Sum = " + sum); } }