Skip to content

Latest commit

 

History

History
22 lines (15 loc) · 962 Bytes

e093.md

File metadata and controls

22 lines (15 loc) · 962 Bytes

Back to questions

Solution to e093: Average of numbers

See code at solutions/code/tutorialquestions/questione093

In the solution I define a class IntegerReader that can be used to read integers, and also specifically positive integers, from standard input. Method readInteger uses a BufferedReader to repeatedly read lines from standard input until a line consisting of simply an integer is entered. The method uses Integer.parseInt to try to convert a line of input to an integer. If the line is not an integer, an NumberFormatException will be thrown. This is immediately caught, and a message is displayed asking the user to enter something that is actually an integer.

The class also declares readPositiveInteger, which repeatedly calls readInteger until a positive integer is entered. Notice the use of a for loop of the form:

for(;;) {
  ...
}

in readPositiveInteger.