Assignment 12

Task

Write a program that reads in an arbitrary number of integers and then displays the total, the average, and the lowest number entered.

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 ints 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) an int, 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, their average (as a double), and the smallest (closest to negative infinity) number entered.

Do not use any arrays, ArrayLists, or other Collections to complete this assignment.

Sample Output

D:\TA\grading\A12>java ZtomaszeA12
This program will average a series of integers.
Enter a number (or nothing to stop): 10
Total: 10
Enter a number (or nothing to stop): 5
Total: 15
Enter a number (or nothing to stop): 12.2
Bad input: please enter only integers (or, to quit, just hit enter).
Enter a number (or nothing to stop): -3
Total: 12
Enter a number (or nothing to stop): quit
Bad input: please enter only integers (or, to quit, just hit enter).
Enter a number (or nothing to stop): stop
Bad input: please enter only integers (or, to quit, just hit enter).
Enter a number (or nothing to stop): 1 more
Bad input: please enter only integers (or, to quit, just hit enter).
Enter a number (or nothing to stop): 4
Total: 16
Enter a number (or nothing to stop):

Total: 16
Average: 4.0
Lowest number entered: -3

D:\TA\grading\A12>java ZtomaszeA12
This program will average a series of numbers.
Enter a number (or nothing to stop): 9
Total: 9
Enter a number (or nothing to stop): 4
Total: 13
Enter a number (or nothing to stop): 13
Total: 26
Enter a number (or nothing to stop):

Total: 26
Average: 8.666666666666666
Lowest number entered: 4

D:\TA\grading\A12>java ZtomaszeA12
This program will average a series of numbers.
Enter a number (or nothing to stop):

You did not enter any numbers.

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.

Hints

What to Submit

Upload your UsernameA12.java file to Tamarin.

Grading [5 points]

1 - Compiles + Coding Standards
Your program compiles successfully (no errors). Your code follows Java coding standards.
1.5 - Read loop
Your program reads in an arbitrary number of integers (0.5). The program does not crash if given something other than an int (0.5). The loop ends if the user enters nothing (0.5).
2.5 - Results
You print the total (0.5) and average (as a double) (1.0), and lowest value (1.0), each clearly labelled. (By "lowest", I mean the entered value that is closest to Integer.MIN_VALUE. You can label this as "lowest" or "smallest".) Results are still correct even when incorrect input is handled. You did not use an array or other list (-1 if you did).

FAQs

Problem set from lab
Section 001 & 003: Use a loop to do the following:
  1. Parrot: echo the user's input until the user enters nothing.
  2. Print all the words to the song "99 Bottles of Beer"
  3. Print all the powers of 2 that are less than 5000
  4. Print backwards all the positive even integers starting with 100. (That is: 100, 98, 96, 94.... 2.)
  5. Print the numbers 1 through 10 on one line, with a comma after each except the last one. So, the list should look exactly like: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
  6. Hi-Lo number game: The computer picks an int between 1 and 100 and then tells the user whether each guess is too high or low until the user guesses the number correctly.

Challenge problems (for more practice or if you finish the above early):
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.) Or see if the length of the string is 0. (Please do not use the isEmpty() method, though; it's not available in Java 5, so your code won't compile on Tamarin.)
Argh, I'm stuck!
First, have a look at the sample solutions (when posted) to the problem set above. In particular, the first problem involves a loop that needs to stop when the user enters nothing.

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. Then add the error handling for times when the String cannot be turned into a number.

So, in preparation for the finished program, you want to make sure you handle three situations: the user enters an integer, something that is not an integer, or nothing. On a number, print the total (which, for one number, will be the same as the number itself). On a word, print an error message. On nothing, print nothing (for now).

So, to produce the output I just described, you'll have the following pieces of code (though not necessarily in this order):

Now, once you get this compiled and running, test that you get the output described for each of the three kinds of input. Once you do, this is the code you want to repeat over and over in a loop. So then see if you can wrap a while loop around this somehow. Ideally, you will still have each element of code occur only once. That is, even when using a loop, it is possible (though not required) to still have one call to nextLine(), one try/catch, one call to parseInt, and so on.

Note that you probably won't print the final results until after the loop.

How does parseInt work again?
String input = ...;
int num = Integer.parseInt(input);
Remember that it might throw a NumberFormatException if input cannot be converted to a meaningful number. Both the exception and the Integer 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.) Since you also won't be able to correctly print the lowest value entered if the user entered 0 numbers, I recommend using a conditional to check that your number count isn't 0 before trying to print the final results.
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.