/home/wpollock1/public_html/Java/ScannerDemo2.java

// Scanner Demo 2, showing how to skip over bad data.
// In this dumb console application, you just enter a series of integers,
// one per line.  These are summed and the total displayed at the end.
//
// Try entering in multiple items on one line.  Type EOF (^Z or ^D) or ^C
// to exit the while loop.  For error control, use hasNext(pattern) to ensure
// each line only contains a single number.
//
// Written 9/2015 by Wayne Pollock, Tampa Florida USA

import java.util.Scanner;

class ScannerDemo2 {
   public static void main ( String [] args ) {
      Scanner in = new Scanner( System.in );
      int total = 0;
      System.out.print( "\nEnter a number (type ^Z to exit): " );
      while ( in.hasNextLine() ) {
         Scanner s = new Scanner( in.nextLine() );
         if ( s.hasNextInt() ) {
            int num = s.nextInt();
            total += num;
            System.out.println( "You entered " + num );
         } else {
            System.out.println( "--Skipping bad item--" );
         }
         System.out.print( "Enter a number: " );
      }
      System.out.println( "\nTotal: " + total );
   }
}