/* T01n12.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 first 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. * * And, yes, I'm jumping the gun a bit by introducing a user-defined method * to these examples. We'll be covering those soon, and I think you'll * be able to make sense out of this now, given your backgrounds in other * programming languages. */ import java.util.*; public class T01n12 { 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); // What do you think of this line? 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 scans the message character by * character, and builds up the new string almost character by * character. Note that no StringBuilder objects are used * (or harmed) in the execution of this program. * * For fun, try initializing excited to null instead of to the * empty string, and run the program. */ private static String addExcitement (String original) { String excited = ""; // Reference to built-up resulting message char currentChar; // Holds each char of orig. message in turn for (int i=0; i < original.length(); i++) { currentChar = original.charAt(i); if (currentChar == '.') { excited = excited + '!'; } else if (currentChar == '!') { excited = excited + "!!"; } else { excited = excited + currentChar; } } return excited; } }