------------------------------------------------------------------------------
MC logo
I/O Exception Handling
[^] Semantics
------------------------------------------------------------------------------
[Ch. 1: Overview and History] [Syntax] [Names and Scope] [Types and Type Systems] [Semantics] [Functions] [Memory Management] [Imperitive Programs and Functional Abstraction] [Modular and Class Abstraction] [Functional Programming] [Logic Programming]
[Copy v. Reference Assignment] [Return Value and Exceptional I/O]
#include <stdio.h>

/*
 * Total input consisting of integers, skipping bad data
 */

int main(int argc, char **argv)
{
        int sum = 0;            /* Sum of input ints. */
        int ct = 0;             /* How many summed? */
        int errors = 0;         /* Num conv errors.*/

        /* Read all the input. */
        while(1) {
                /* Try to read an integer, then see what
                   happened. */
                int intin;
                int res = scanf("%d", &intin);

                /* And end-of-file, we're done. */
                if(res == EOF) break;

                /* A return of 0 means a conversion 
                   error */
                if(res == 0) {
                        /* Skip to the next digit. */
                        scanf("%*[^0-9-]");
                        ++errors;
                        continue;
                }

                /* A return of 1 means success.  */
                if(res == 1) {
                        sum += intin;
                        ++ct;
                        continue;
                }
        }

        /* Print the results. */
        printf("%-10s %5d\n%-10s %5d\n%-10s %5d\n",
               "sum:", sum, "ct:", ct, 
               "errors:", errors);

import java.util.*;
import java.lang.RuntimeException;

/*
 * Total input consisting of integers, skipping bad data
 */
class Addem 
{
    public static void main(String[] args)
    {
        int sum = 0;            // Sum of input integers.
        int ct = 0;             // How many were summed.
        int errors = 0;         // Num conv errors.

        // Standard class that reads and converts items 
        // in the input.
        Scanner in = new Scanner(System.in);

        // This makes the scanner behave more like C 
        // scanf.  It lets a number end with any non-digit.
        // Still, with a little thought, I expect you can
        // find some input on which the two programs disagree.
        in.useDelimiter("[^0-9-]\\s*");

        try {
            // Read all the input.  The 
            // NoSuchElementException gets us out 
            // of the loop, which can be thrown by either 
            // in.nextInt() or in.skip().
            while(true) {
                // Attempt to read the next integer.
                int next;
                try {
                    sum += in.nextInt();
                    ++ct;
                }
                catch(InputMismatchException e) {
                    // This is a bad conversion.  
                    // We'll count it, and we need to skip 
                    // the bad stuff.
                    ++errors;
                    in.skip("[^0-9-]+");
                }
            }
        }
        catch(NoSuchElementException e) {
            // This is thrown at eof.  Print results.
            System.out.printf
                ("%-10s %5d\n%-10s %5d\n%-10s %5d\n",
                 "sum:", sum, "ct:", ct, 
                 "errors:", errors);
        }
    }
Both these programs read input streams and total the integers found, skipping and counting conversion errors. The will give the same output for most input streams, but are not really identical.