import java.io.*; class Average6b { // A program that calculates the average of a series of // integers. It prompts for each one. A sentinel value is // used to indicate the end of the series. // Catches invalid input static final int SENTINEL = -999; public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader (new InputStreamReader(System.in)); int sum=0,count=1,n,fraction; while(true) { System.out.print("Enter number "+count); System.out.print(" (or "+SENTINEL+" to finish): "); try { n=Integer.parseInt(in.readLine()); if(n==SENTINEL) break; sum+=n; count++; } catch(NumberFormatException e) { System.out.println("Not a valid number"); } } System.out.print("Average is: "+sum/(count-1)+"."); fraction = (sum*100/(count-1))%100; if(fraction<10) System.out.println("0"+fraction); else System.out.println(fraction); } }