Write a program that reads a series of numbers from System.in
and computes and prints their average, but with a couple of twists.
First twist: The numbers are entered one per line. If a line contains anything other than a number surrounding by blanks or if there are no more input lines, the program should stop and compute the average of numbers read so far and then terminate. If no numbers have been entered, your program should print a scolding message.
Second twist: Do not use any if
or switch
statements to write this
program. Use exception handlers for determing if there is any more
input and if the input line is a valid number.
You can use one while
loop, but the condition for the
loop must be true
.
Use methods from
Integer
,
Scanner
, and
String
in your program.