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

Lab Solution

Be sure to put this in a file called "Names.java".

/*
 * Names.java
 *
 * Created on August 7, 2003, 8:11 PM
 */

import javax.swing.*;

/**
 *
 *
 * @author  JonesB
 */
public class Names {
    
    String names[];
   
   
    /** Creates a new instance of Names */
    public Names() {
    }
   
    public void run() {
        promptNames();
        printNames();
    }
   
    public void promptNames() {
        String numPets = JOptionPane.showInputDialog("How many pets do you have?");
        int intNumPets = Integer.parseInt(numPets);
        names = new String[intNumPets];

        for (int i = 0; i < intNumPets; i++) {
            names[i] = JOptionPane.showInputDialog("What is the name of pet " + (i + 1) + "?");
        }
    }
   
    public void printNames(){
       
        for ( int j = 0; j < names.length; j++) {
            System.out.println("Pet name: " + names[j]);
        }
    }
   
   
    public static void main(String[] args) {
     Names n = new Names();
     n.run();
    }
   
   
}


Created by:  Brandan Jones January 4, 2002