// // Exceptional String Cleaning :) // public class cleanString { public static String toString(String source) throws IllegalCharactersException, FirstLetterNotCapitalizedException, SentenceNotPeriodTerminatedException { int i, len = source.length(); StringBuffer dest = new StringBuffer(len); if (!isCapitalLetter(source.charAt(0))) { throw new FirstLetterNotCapitalizedException("First letter of sentence not capitalized."); } if (source.charAt(source.length()-1) != '.') { throw new SentenceNotPeriodTerminatedException("Sentence not period terminated."); } for (i = 0; i < (len-1); i++ ) { // all but the trailing "." if (isLegal(source.charAt(i))) { dest.append(cleanLetter(source.charAt(i))); } else { throw new IllegalCharactersException("Illegal character in sentence"); } } return dest.toString(); } static boolean isLegal(char letter) { // // Definition of a legal char: // // - Upper/lower A-Z // - blank // if (letter >= 'a' && letter <= 'z') return true; if (letter >= 'A' && letter <= 'Z') return true; if (letter == ' ') return true; return false; } static char cleanLetter(char letter) { if (letter >= 'A' && letter <= 'Z') return ((char) ((letter - 'A') + 'a')); return(letter); } static boolean isCapitalLetter(char letter) { if (letter >= 'A' && letter <= 'Z') return true; return false; } }