import java.util.ArrayList; import java.util.Scanner; /** * Created with IntelliJ IDEA. * User: hasker * Date: 11/7/13 * Time: 10:11 AM * To change this template use File | Settings | File Templates. */ public class Roster { private ArrayList names = new ArrayList(); public void readNames(String prompt) { System.out.print(prompt); names.clear(); Scanner name_input = new Scanner(System.in); String next_name = name_input.next(); while ( !next_name.equals("END")) { add(next_name); next_name = name_input.next(); } } public void add(String new_name) { names.add(new_name); } public void printNames() { for(String nameFromList : names) { System.out.println(nameFromList); } } public int occurrencesOf(String target) { int count = 0; // note could use the for-each loop here as well for(int i = 0; i < names.size(); i++) { String someName = names.get(i); if ( someName.equals(target) ) count++; } return count; } /** * Convert all names in the list so that the first letter is capitalized * and the remaining letters are in lower case. We recognize this is not * appropriate for all names, but the code is designed to illustrate * modifying the array. */ public void convertNamesToProperCase() { for(int i = 0; i < names.size(); i++) { String myName = names.get(i); String result = ""; if ( myName.length() > 0 ) result += Character.toUpperCase(myName.charAt(0)); for(int name_ix = 1; name_ix < myName.length(); name_ix++) { char asLower = Character.toLowerCase(myName.charAt(name_ix)); result += asLower; } names.set(i, result); } } public boolean hasName(String target) { return names.contains(target); } }