/** * Creates and displays an array of Strings * * @author William McDaniel Albritton */ public class Stooges { /** * main method starts the program * * @param args are not used */ public static void main(String[] args) { //method call that creates an array String [] stoogesNames = Stooges.createStooges(); //method call that displays an array Stooges.displayArray(stoogesNames); } /** * creates and returns an array of strings * * @return an array of the 3 Stooges names */ public static String [] createStooges(){ // declare array and instatiate (create) array String [] stooges = new String[3]; //initialize integer variable Integer stringLength = new Integer(0); //bug: will throw a java.lang.NullPointerException //stringLength = stooges[0].length(); //initialize array (assign values to each array element) stooges[0] = new String("Larry"); stooges[1] = new String("Curley"); stooges[2] = new String("Moe"); //fixed bug: use length() after the string is initialized stringLength = stooges[0].length(); System.out.println("stringLength = " + stringLength); //return array to calling method return stooges; } /** * display array, length of array, length of each string in the array * * @param anArray is the array to be displayed */ public static void displayArray(String [] anArray){ //initialize variables Integer arrayLength = new Integer(0); Integer nameLength = new Integer(0); //get and display the number of elements in the array arrayLength = anArray.length; //not length() System.out.println("array length = " + arrayLength); //loop to display the array and the length of each string in the array for(Integer i = new Integer(0); i < arrayLength;i=i+1){ //number of characters in each string nameLength = anArray[i].length(); //not length System.out.println(anArray[i] + " has " + nameLength + " characters"); } } }