Write a program that reads in an arbitrary number of numbers and then displays the total and the average.
Concepts: while
loops, for
loops; Integer.parseInt()
, Double.parseDouble()
Textbook: 5.5, 5.8; 3.8
Use a loop to allow the user to enter as many double
s as they like. Keep a running total (and count) of the numbers entered. Display the current total after each number entered. If the user enters anything other than (or in addition to) a double, print an error message and ignore that input, but keep reading numbers. If the user enters nothing, the program should stop reading numbers.
Once the user has indicated that they are done entering numbers, print the sum of all the entered numbers and their average.
D:\TA\grading\A12>java ZtomaszeA12 This program will average a series of numbers. Enter a number (or nothing to stop): 10 Total: 10.0 Enter a number (or nothing to stop): 5.5 Total: 15.5 Enter a number (or nothing to stop): 1o Bad input: please enter only numbers (or, to quit, just hit enter). Enter a number (or nothing to stop): oops Bad input: please enter only numbers (or, to quit, just hit enter). Enter a number (or nothing to stop): total ? Bad input: please enter only numbers (or, to quit, just hit enter). Enter a number (or nothing to stop): 9.8 Total: 25.3 Enter a number (or nothing to stop): 1.1 Total: 26.400000000000002 Enter a number (or nothing to stop): Total: 26.400000000000002 Average: 6.6000000000000005 D:\TA\grading\A12>java ZtomaszeA12 This program will average a series of numbers. Enter a number (or nothing to stop): 5 Total: 5.0 Enter a number (or nothing to stop): Total: 5.0 Average: 5.0 D:\TA\grading\A12>java ZtomaszeA12 This program will average a series of numbers. Enter a number (or nothing to stop): Total: 0.0 Average: 0
Note how the user might not enter a number at all; or they many enter multiple errors in a row. Your program should still handle this gracefully. Also, note that whenever you do math with doubles, there's always the possibility that the result will not be exact. You don't have to worry about that. (You can if you want to though: check out java.text.DecimalFormat
or printf
; see Chapter 3.6 of your textbook for more.)
nextDouble()
to read in the numbers for this one. If you did, the program would hang until the user entered one or more non-white-space characters.
nextLine()
to read in everything they typed. If this is an empty string, they mean to quit; otherwise, turn it into an double using Double.parseDouble()
. Remember that Double.parseDouble()
throws a NumberFormatException
if it cannot turn the given string into a double
.
Upload your UsernameA12.java
file to Tamarin.
double
(0.5). The loop ends if the user enters nothing (0.5).
int letter = 'a'; letter++;
)
switch
)
I suggest looking at the solutions only after you've done the problems (or can't figure out how to do them). Remember, there's always more than one way to do it, so you may come up with some solutions that differ wildly from those given in this file--that's fine, as long as they produce correct output. You should also be able to understand how my solutions work too, though.
isEmpty()
method, though; it's not available in Java 5, so your code won't compile on Tamarin.)
It usually helps me to write a hard program one step at a time, where I compile, run, and test after each step. With loops, I often find myself working from the inside out. Ignore the loop to start with and just try to get your program to compile/work for only a single number: read in the user's input as a String and then convert it to a number. Maybe then add the error handling for times when the String cannot be turned into a number.
Now, once you get this compiled and running, this is the code you want to repeat over and over. So see if you can wrap a while loop around it. You want the loop to stop when the String input (before you convert it to a number) is empty. Of course, you may find that you read the string in half-way through the loop body, so you'll probably also need to use an if/else somehow in the while loop body to avoid trying to convert the empty string into a number and adding it to the total.
parseDouble
work again?
String input = ...; double num = Double.parseDouble(input);Remember that it might throw a
NumberFormatException
if input
cannot be converted to a meaningful number. Both the exception and the Double
class are in java.lang
, so no import is required to use them.
NaN
if the user enters no numbers.
ArithmeticException
.) This is acceptable output. If you want to avoid it, just use a conditional to check that your number count isn't 0 before you try dividing.