// purpose: demonstrate string replacement import java.util.*; public class Replacement { // method main(): program starting point public static void main( String[] args ) { // get a scanner for input Scanner stdin = new Scanner( System.in ); // get text from user System.out.print( "Enter text: " ); String text = stdin.nextLine(); // get text to be replaced and its replacements System.out.print( "Enter two strings: " ); String s1 = stdin.next(); String s2 = stdin.next(); System.out.print( "Enter two more strings: " ); String s3 = stdin.next(); String s4 = stdin.next(); String revised1 = text.replaceFirst( s1, s2 ); String revised2 = text.replaceAll( s3, s4 ); System.out.println( "Original text: \n\t" + text ); System.out.println( "\nEdited text (in original replace first " + s1 + " with " + s2 + "):\n\t" + revised1 ); System.out.println( "\nEdited text (in original replace all " + s3 + "'s with " + s4 + "'s):\n\t" + revised2 ); System.out.println(); } }