Back to 111 Main Page

Mobius strip

Assignment 11

Task

Write a program that reads in an arbitrary number of numbers and then displays their average.

Concepts: while loops, for loops; Integer.parseInt(), Double.parseDouble()
Textbook: 5.5, 5.8; 3.8

Steps

Use a loop to allow the user to enter as many doubles 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.

Sample Output

D:\TA\grading\A11>java ZtomaszeA11
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\A11>java ZtomaszeA11
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\A11>java ZtomaszeA11
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. Don't worry about that.

Hints

  • You won't be able to use 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.
  • Instead, use 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.

What to Submit

Upload your UsernameA11.java file to Tamarin.

Grading [4 points]

1 - Compiles + Coding Standards
Your program compiles successfully (no errors). Your code follows Java coding standards.
2 - Read loop
Your program reads in an arbitrary number of doubles (1.0). The program does not crash if given something other than a double (0.5). The loop ends if the user enters nothing (0.5).
1 - Results
You print the total (0.5) and average (0.5), clearly labelled.

FAQs

Where is the demo code from lab?
Here: LoopExamples.java

This file contains some sample solutions to the problems given in lab, as well as a couple problems not mentioned in lab. The problems are listed at the top of the file, so I recommend you try to do each one and then scroll down to look at the solutions only after you've done it (or can't figure out how to do it). 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, if they produce correct output. You should also be able to understand how my solutions work too, though.

How do I tell if the user entered nothing?
Compare the String that they entered to the empty String: "". (That's a String with zero characters in it.)
Argh, I'm stuck!
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. After you read in the user's input, print the number to the screen if it really is a number, print your error message if it's not a number, or print "quit" if it's an empty string.

Then, once you have this working, add the loop so it does the above as long as you keep entering input. (This will be an infinite loop, but your output should now look something like the sample output above--except it will be printing the numbers you entered and just prints "quit" rather than acutally quitting. Remember, Ctrl-c will kill an infinite loop.)

Then change your program from just printing "quit" if the user enters nothing to actually stopping the loop.

Now your input loop is done and you can work on printing out the average.
How does 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.
My average comes out as NaN if the user enters no numbers.
This is short for "not a number" because you're dividing by 0. (This happens with doubles; remember that dividing ints by 0 causes a runtime crash--an 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.
Things to note:
• Please declare your Scanner before the loop and not in the loop. This way, you only create one Scanner object, rather than creating a new Scanner for each number. Either way will work for you, but creating a new Scanner each time is inefficient.


~ztomasze Index : TA Details: ICS111: A11
http://www2.hawaii.edu/~ztomasze
Last Edited: 24 Feb 2009
©2008 by Z. Tomaszewski.