Allan Didier

Find and Replace

Goals

Write a Java program to simulate the Find and Replace functions found in word processors.

Skills

  • String variables

Program Details

Write a program which can replace a phrase found within a text paragraph with a second phrase. You program needs the following. 

  1. Asks the user to enter a String to search for to be replaced, findThis.
  2. Asks the user to enter another String to replace the above String, replaceWith.
  3. You can hard code or ask the user to enter the paragraph of text to search through, inThis.
  4. Asks the user if they want to replace the first or all occurrences of findThis.
    1. If the user chooses the first occurrence, find the first occurrence of the String, findThis, inside the String , inThis, and replace it with the String, replaceWith.
    2. If the user chooses to replace all, replace all the occurrences of findThis in inThis with replaceWith. 
  5. Output the original paragraph of text along with the new paragraph. 
  6. If the search string, findThis, is not found in the paragraph, inThis, report that the string was not found. 

 

Resources

Edhesive T1 Lesson 21: Strings

Edhesive T1 Lesson 22: String Methods

AP College Board 2.6: String Objects

AP College Board 2.7: String Method