1. Technology

Going Loopy Example Program


Going Loopy Example Program

Here is example code showing the use of a for loop, a do..while loop and a while loop. The program uses a JFrame and JTextArea to provide feedback to the user.

First it asks the user to pick a number between 1 and 15. Once picked it will display the factorial for the number picked. The factorial is calculated by using a for loop.

Next, the program asks for a number between 1 and 20. A do..while loop is used to keep asking the user for a number until it is between 1 and 20.

Lastly a while loop is used to count between the number entered up to the number 20.

The articles relating to this example code are Determinate Loops and Indeterminate Loops.

//Imports are listed in full to show what's being used
//could just import javax.swing.* and java.awt.* etc..
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

public class GoingLoopy {

    //Note: Typically the main method will be in a
    //separate class. As this is a simple one class
    //example it's all in the one class.
    public static void main(String[] args) {
         //Use the event dispatch thread for Swing components
         EventQueue.invokeLater(new Runnable()
             public void run()
                 new GoingLoopy();         
    //handles the creation of the JFrame and
    //all it's components
    public GoingLoopy()
        JFrame guiFrame = new JFrame();
        //make sure the program exits when the frame closes
        guiFrame.setTitle("Going Loopy");
        //This will center the JFrame in the middle of the screen
        //Using a JTextArea to display the output
        //from the loops
        JTextArea tracker = new JTextArea("Tracking the loops:\n");
        //Options for Numbers between 1 and 12 input dialog
        //Note: The reason this is an array of Integer objects rather than
        //just an int array is because JOptionPane's .showInputDialog method
        //expects an object array for seletion values. 
        Integer[] oneToTen = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
        //Input dialog box allowing the user to pick number.
        Integer numberPicked = (Integer)JOptionPane.showInputDialog(null, "Pick a Number:"
                , "Factorial", JOptionPane.QUESTION_MESSAGE
                , null, oneToTen, oneToTen[0]);
        //Calculate the factorial of the number chosen
        int factorial = 1;
        //Use a for loop becuase we can determine how many times we need to 
        //perform the calculation
        for (int i=1; i <numberPicked+1; i++)
            factorial = factorial * i; 
        tracker.append("The factorial of " + numberPicked + " is: " + Integer.toString(factorial)+ "\n");
        //We can use an indterminate while loop to keep asking the user to enter 
        //in a number. We don't know how many times we have to ask so a while loop
        //is a good option.
        boolean isNumber = false;
        int number = 0;
            //Input dialog box allowing the user enter a number.
            String numberEntered = JOptionPane.showInputDialog(null, "Enter in a number between 1 and 20:"
                , "Number Validation", JOptionPane.QUESTION_MESSAGE);
                number = Integer.parseInt(numberEntered);
                if (number > 1 && number < 21)
                    isNumber = true;
                    tracker.append("Congratulations you entered in the number " + number + "\n");
                    tracker.append("You didn't enter in a number between 1 and 20 \n");
            catch(NumberFormatException e)
                tracker.append("You didn't enter in a number\n");
        //Now that we have a number we can use a while loop to display all the
        //numbers between it and 20
        while (number <20)
            tracker.append("Counting up to 20.... I'm at.." + number + "\n");
See More About
  1. About.com
  2. Technology
  3. Java
  4. Java Syntax
  5. Control Flow
  6. Java Syntax: Going Loopy Example Program

©2014 About.com. All rights reserved.