import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.StringTokenizer; public class Rolodex { public static void main( String[] args ) throws IOException { FileReader charStream = new FileReader( "Rolodex.in" ); BufferedReader in = new BufferedReader( charStream ); String output = ""; String line = in.readLine(); int count = 0; int ohs = 0; // count of the letter oh while( line != null ) { placeContactInArrayList( line ); printMoneyOwedMe( list, count ); // count all the letter ohs in the database ohs = ohs + countAllLettersOh( line ); // gather all words and their lengths into one outprint string output = output + getNumberOfLettersInEachWord( line ); // search and select only iou fields > 400 printNamesOfThoseOwingOver400Dollars(); count++; // keep count of the ArrayList cell number. line = in.readLine(); } // end while System.out.println( "There are " + ohs + " occurances of the letter \"o\" " + "\n\n" ); System.out.println( "The number of letters in each word are: \n" + output ); } // end main() private static void printNamesOfThoseOwingOver400Dollars() { ArrayList bigDoner = list; int len = bigDoner.size(); // length of ArrayList // walk through ArrayList for( int i = 0; i < len; i++ ) if( bigDoner.get( i ).iou > 400 ) System.out.println( "Big Doner = " + bigDoner.get( i ).firstName + " " + bigDoner.get( i ).surName ); } // end printNamesOfThoseOwingOver400Dollars() private static String getNumberOfLettersInEachWord( String db ) { StringTokenizer st = new StringTokenizer( db ); String word = "", result = ""; while( st.hasMoreTokens() ) { word = st.nextToken(); result = result + word + "\t" + word.length() + "\n"; } // end while ( st.hasMoreTokens() ) return result; } // end getNumberOfLettersInEachWord( ) // use String.charAt() static public int countAllLettersOh( String record ) { int len = record.length(); // length of String object int numberOfOhs = 0; // use String.charAt(index) to return character at location index for( int i = 0; i < len; i++ ) if( record.charAt( i ) == 'o' ) numberOfOhs++; return numberOfOhs; } // countAllLettersOh( ) static public void printMoneyOwedMe( ArrayList scroll, int index ) { // assign values of array cell to local variables String first = scroll.get( index ).firstName; String last = scroll.get( index ).surName; String town = scroll.get( index ).city; double receivable = scroll.get( index ).iou; // print using local variables. System.out.println( first + " " + last + " of " + town + " owes me " + receivable ); // print using instance (global) variables. System.out.println( scroll.get( index ).firstName + " " + scroll.get( index ).surName + " of " + scroll.get( index ).city + " owes me " + scroll.get( index ).iou + "\n\n" ); // Output from local and global variables is same here. } // end printNumberOfCharsPerField() static void placeContactInArrayList( String record ) { StringTokenizer st = new StringTokenizer( record ); ContactRecord temp = new ContactRecord(); ArrayList localList = list; while( st.hasMoreTokens() ) { temp.firstName = st.nextToken(); temp.surName = st.nextToken(); temp.street = st.nextToken(); temp.city = st.nextToken(); temp.prov = st.nextToken(); temp.phone = Long.parseLong( st.nextToken() ); temp.iou = Double.parseDouble( st.nextToken() ); localList.add( temp ); } // end while ( st.hasMoreTokens() ) } // end placeContactInArrayList() static private class ContactRecord { public String firstName; public String surName; public String street; public String city; public String prov; public long phone; public double iou; } // end class ContactRecord static private ArrayList list = new ArrayList(); } // end class Rolodex