/* T01n13.java -- Given a message typed by the user, find all periods and * replace them with exclamation points, and find all exclamation points * and add another exclamation point after each one. For example: * Hey! Slow down. ---> Hey!! Slow down! * * This is the second of three examples that do this same task, but in * different ways: * * T01n12.java -- Using Strings, build the new string a character at a time * T01n13.java -- Still using only Strings, use substring() to build the * new string in chunks (uses fewer concatenations). * T01n14.java -- Convert the input into a StringBuilder object and * manipulate it directly. * * Can you think of a more efficient way to locate the next period or * exclamation point? */ import java.util.*; public class T01n13 { public static void main (String [] args) { String message; // User's boring/exciting message Scanner keyboard = new Scanner (System.in); // Input is from keyboard System.out.print("Please type in a message : "); message = keyboard.nextLine(); message = addExcitement(message); System.out.println("\nThank you. If you had been more excited, " + "you'd have typed your message\nlike this:\n\t" + message); } /* This version of addExcitement builds the new string a group * of characters at a time. We locate the next . or !, and then * add the whole group of characters ahead of it to the new string * before adding the ! or the !! as appropriate. We contine until * no more . and ! remain, and finish by adding the remaining * characters to the new string and returning it. */ private static String addExcitement (String original) { String excited = ""; // Reference to built-up resulting message int currentChar = 0, // *Index* of current punctuation character firstPeriod, // closest subsequent (.) w.r.t. current loc. firstExclamation, // closest subsequent (!) w.r.t. current loc. earliest; // location of closest subsequent (.) or (!) while (true) { // Starting where we left off, find the next // occurrences of . and ! firstPeriod = original.indexOf('.',currentChar); firstExclamation = original.indexOf('!',currentChar); // If we can't find any more of either, leave loop if (firstPeriod < 0 && firstExclamation < 0) { break; } else { // At least one . or ! exists in the remainder of // the user's message. Find the location of the // closest one. if (firstPeriod >=0 && firstExclamation >= 0) { earliest = Math.min(firstPeriod,firstExclamation); } else if (firstPeriod >=0 && firstExclamation < 0) { earliest = firstPeriod; } else { earliest = firstExclamation; } // Add the group of characters ahead of the // . or ! to the new string excited += original.substring(currentChar,earliest); // Add excitement by converting the characters if (original.charAt(earliest) == '.') { excited += '!'; } else { excited += "!!"; } // Move beyond the . or ! we just dealt with currentChar = earliest + 1; } } // The characters after the last . or ! still need // to be added to the new string to complete our work. excited += original.substring(currentChar,original.length()); return excited; } }