import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.StringTokenizer; import java.util.ArrayList; public class Compress { public static void main( String[] args ) throws IOException { // BufferedReader captures data from data file on disk drive BufferedReader in = new BufferedReader( new FileReader( "Test.in" ) ); StringTokenizer st = null; String output = ""; String line = in.readLine(); // read new line of data while( line != null ) // detects end of file when line == null { st = new StringTokenizer( line ); while( st.hasMoreTokens() ) { placeTokenInAppropriateArrayList( st.nextToken() ); } // end while output = getOutputFromList( words ); output = output + getOutputFromList( punct ); System.out.println( output ); line = in.readLine(); // read string from file } // end while } // end main( ) static String getOutputFromList( ArrayList list ) { String word = "", output = ""; int count = 0, index = 0; while( !list.isEmpty() ) { word = list.get( 0 ); count = 1; list.remove( 0 ); // Returns -1 if word is not in the list. while( list.indexOf( word ) != -1 ) { index = list.indexOf( word ); count++; list.remove( index ); } // end while output = output + count + word; } // end while return output; } // end getOutputFromList() static void placeTokenInAppropriateArrayList( String tok ) { int len = tok.length(); char ch; String word = ""; for( int i = 0; i < len; i++ ) { ch = tok.charAt( i ); // grab next character if( Character.isLetter( ch ) ) word = word + ch; else punct.add( "" + ch ); // add now as length is never > 1 } // end for if( word != null ) words.add( word ); } // end placeTokenInAppropriateArrayList( ) static private ArrayList words = new ArrayList(); static private ArrayList punct = new ArrayList(); } // end class Compress ///////////////////////////////////////////////////// // // Screen Dump // =========== // // 3nullYES3nullI3nullCAN3! // 1nullSHE1nullLOVES1nullYOU3nullYEAH4! // 3nullSEA2nullSHELLS1nullBY1nullTHE1nullSHORE1. // /////////////////////////////////////////////////////