I was able to reconstruct your error by entering " 23 32" on a single line:
Enter positive integers, one number perline, ending with ..1
23 32
Exception in thread "main" java.lang.NumberFormatException: For input string: " 23 32"
Then Integer.parseInt(keyboard.readLine());
tries to parse an Integer from " 23 32" what gives error as only digit characters are expected. Maybe try to using Scanner.nextInt()
(http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html) so that you don't need to care about whitespaces.
Also note that average = sum / count
will give you wrong answer:
Enter positive integers, one number perline, ending with ..1
5
6
-1
The sum of the number is: 11
The average is: 5.0
as sum
and count
are both integers so integer division will be applied. In this case 11/2 = 5
. Therefore you should use average = (double) sum / (double) count
.
And a final note. It's a good practice to initialize variables at declaration time:
int sum = 0;
int count = 0;