import java.io.*; import java.util.StringTokenizer; class Prob1 { public static void main( String [] args ) { System.out.print( "Input File: " ); String inputFile = UserInput.readString(); System.out.print( "Output File: " ); String outputFile = UserInput.readString(); //------------------------------------ BufferedReader rd = null; BufferedWriter wr = null; try { rd = new BufferedReader( new FileReader( inputFile ) ); } catch ( FileNotFoundException e ) { System.out.println( "cannot open the read file: " + inputFile + "; Error: " + e ); } try { wr = new BufferedWriter( new FileWriter( outputFile ) ); } catch ( IOException e ) { System.out.println( "cannot open the write file: " + outputFile + "; Error: " + e ); } //------------------------------------ System.out.println( "THE READ FILE CONTAINS:" ); String data = null; try { while ( (data = rd.readLine()) != null ) { System.out.println( data ); } } catch ( IOException e ) {} //------------------------------------ System.out.println( "Enter anything you want and it will be right out to the file." ); System.out.println( "Enter \"I quit\" to exit the program." ); try { do { data = UserInput.readString(); wr.write( data ); wr.newLine(); } while ( !data.equalsIgnoreCase( "I quit" ) ); } catch ( IOException e ) {} //------------------------------------ try { rd.close(); wr.close(); // What would happen if this statement is commented out? } catch ( IOException e ) { System.out.println( "Error Closing Files: " + e ); } } } class Prob2to4 { public static void main( String [] args ) { String testString = " \tTest trim string! "; System.out.println( "Test Triming String (own algo)" ); System.out.println( "------------------------------" ); String str1 = trimString_OwnAlgo_Iteratively( testString ); System.out.println( "I/P: " + testString ); System.out.println( "O/P: " + str1 ); System.out.println( "\nTest Triming String (trim method)" ); System.out.println( "---------------------------------" ); str1 = testString.trim(); System.out.println( "I/P: " + testString ); System.out.println( "O/P: " + str1 ); testString = " \tTest removing white space characters! "; System.out.println( "\nTest Removing Whitespaces from String (own algo)" ); System.out.println( "------------------------------------------------" ); str1 = removeAllWhiteSpacesFromString_OwnAlgo_Iteratively( testString ); System.out.println( "I/P: " + testString ); System.out.println( "O/P: " + str1 ); System.out.println( "\nTest Removing Whitespaces from String (use StringTokenizer)" ); System.out.println( "-----------------------------------------------------------" ); str1 = removeAllWhiteSpacesFromString_StringTokenizer_Iteratively( testString ); System.out.println( "I/P: " + testString ); System.out.println( "O/P: " + str1 ); System.out.println( "\nTest Removing Whitespaces from String (own algo recursively)" ); System.out.println( "------------------------------------------------" ); str1 = removeAllWhiteSpacesFromString_OwnAlgo_Recursively( testString ); System.out.println( "I/P: " + testString ); System.out.println( "O/P: " + str1 ); } public static String trimString_OwnAlgo_Iteratively( String str ) { String outStr = ""; // Find the first non white space at the start of string int startIdx = 0; for ( int i = 0; i < str.length(); ++i ) { if ( str.charAt( i ) != ' ' && str.charAt( i ) != '\t' ) { startIdx = i; break; } } // Find the first non white space at the start of string int endIdx = str.length() - 1; for ( int i = str.length()-1; i >= 0; --i ) { if ( str.charAt( i ) != ' ' && str.charAt( i ) != '\t' ) { endIdx = i; break; } } for ( int i = startIdx; i <= endIdx; ++i ) { outStr += str.charAt( i ); } return outStr; } public static String removeAllWhiteSpacesFromString_OwnAlgo_Iteratively( String str ) { String outStr = ""; for ( int i = 0; i < str.length(); ++i ) { if ( str.charAt( i ) != ' ' && str.charAt( i ) != '\t' ) { outStr += str.charAt( i ); } } return outStr; } public static String removeAllWhiteSpacesFromString_OwnAlgo_Recursively( String str ) { return removeAllWhiteSpacesFromString_OwnAlgo_Recursively(str, "", 0); } public static String removeAllWhiteSpacesFromString_OwnAlgo_Recursively( String str, String outStr, int index ) { if(index == str.length()) return outStr; else { if ( str.charAt( index ) != ' ' && str.charAt( index ) != '\t') { return removeAllWhiteSpacesFromString_OwnAlgo_Recursively(str, outStr + str.charAt( index ), index + 1); } else return removeAllWhiteSpacesFromString_OwnAlgo_Recursively(str, outStr , index + 1); } } public static String removeAllWhiteSpacesFromString_StringTokenizer_Iteratively( String str ) { String outStr = ""; StringTokenizer st = new StringTokenizer( str ); while ( st.hasMoreTokens() ) { outStr += st.nextToken(); } return outStr; } }