Class 1, Part 2 Intro to Java 30-IT-396  

Loops Example

/*
 * LoopMe.java
 *
 * Created on January 14, 2002, 9:29 PM
 */

// import the java.util package so that we can use the Vector class.
import java.util.*;
// import the javax.swing package so that we can use JOptionPane.
import javax.swing.*;

/**
 *
 * @author  default
 */
public class LoopMe {

    ArrayList average;
    ArrayList total;
 
    /** Creates a new instance of Class */
    public LoopMe () {
        average = new ArrayList();
        total = new ArrayList();
    }

    /**
     * execute
     *
     * This method calls three other methods in the program.
     * You can consider it a controller method.
     *
     * @author jonesbr@email.uc.edu
     * @version 1.0
     */
 
    public void execute() {
        promptAverage();
        promptAdd();
        sendOutput();
 
    }
 
    /**
     * promptAverage
     *
     * This method prompts the user for numbers to average with a do-while loop.
     *
     * @author jonesbr@email.uc.edu
     * @version 1.0
     */
 
    public void promptAverage() {
        // wrap it all in a try-catch block
        try {
 
            // Declare number out here so we can use it in our test.
            String number = new String();
 
            do {
                // Prompt the user.
                // Note the use of the escape character for the embedded double quotes.
                number = JOptionPane.showInputDialog("Enter a number to average, or \"stop\" when you are finished.");

                // An if test to add all of the inputted numbers to the vector.
                if (!number.equalsIgnoreCase("stop")) {
                    average.addElement(new Integer(number));
                }

            } while (!number.equalsIgnoreCase("stop"));
        } catch (NumberFormatException e) {
            // Send an informational message, try again.
            System.out.println("Dummy!  I said enter a number!  Try again.");
            promptAverage();
        }
    }
 
    /**
     * promptAdd()
     *
     * This method prompts users for numbers to add with an infinite loop and a
     * break; statement.
     *
     * @author jonesbr@email.uc.edu
     * @version 1.0
     */
 
    public void promptAdd() {
        // Again, wrap it all in a try-catch block.
        try {
            // an infinite loop, by definition.
            while (true) {
 
            String number = JOptionPane.showInputDialog("Enter a number to total, or \"stop\" when you are finished.");

                // An if test to add all of the inputted numbers to the vector.
                // If "stop" is entered, break out of the loop.
                if (!number.equalsIgnoreCase("stop")) {
                    total.addElement(new Integer(number));
                } else {
                    break;
                }
            }
 
        } catch (NumberFormatException e) {
            // Send an informational message, try again.
            System.out.println("Dummy!  I said enter a number!  Try again.");
            promptAdd();
        }
    }
 
    /**
     * sendOutput
     *
     * Calculates the output from the user's data.
     * Prints it to standard output.
     *
     * @author jonesbr@email.uc.edu
     * @version 1.0
     */
 
    public void sendOutput() {
        // Declare variables to store the results of the calculations.
        int intAverage = 0;
        int intTotal = 0;
 
        // Calculate the average with a for loop.
        for (int i = 0; i < average.size(); i++) {
            intAverage += ((Integer)average.elementAt(i)).intValue();
        }
        // Print it.
        System.out.println("Average: " + intAverage/average.size());
 
        // Calculate the total with a for loop.
        for (int i = 0; i < total.size(); i++) {
            intTotal += ((Integer)total.elementAt(i)).intValue();
        }
        // Print it.
        System.out.println("Total: " + intTotal);
    }
 
    /**
     * public static void main(String args[])
     *
     * called from the command line to start the program.
     *
     * @param args A string array of command line parameters.
     * @author jonesbr@email.uc.edu
     * @version 1.0
     */
 
    public static void main(String args[]) {
        LoopMe loopMe = new LoopMe();
        loopMe.execute();
    }
 
}
 

Dummy!  I said enter a number!  Try again.
Dummy!  I said enter a number!  Try again.
Average: 5
Total: 26

Link for Java I 32-IT-396/3-IT-296: Arrays
Links for Computer Programming I 30-IT-206: Lab Exercise or Back to Agenda


Created by:  Brandan Jones January 4, 2002
Updated September 27, 2004