Allan Didier

Find and Replace


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


  • 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. 
  7. Code Requirements:
    1. For an A on the project you must use the indexOf() and substring() methods to find and replace the strings. These two methods will be tested on the AP exam and you should practice using them. 
    2. If you use the replaceAll() or replaceFirst() methods, you will earn at best a B on the assignment. These methods work, but are not required for the AP exam.



Java Textbook

Chapter 3: Using Classes and Objects

Chapter 5: Conditionals and Loops

Chapter 6: More Conditionals and Loops

CS Awesome Website

Bill Barnum's AP Computer Science A videos

AP College Board test prep videos