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. 
  7. Code Requirements:
    1. You must use the indexOf and the substring methods to find and replace strings. These two methods will be on the AP exam multiple times and you need to learn how to use them.
    2. Java does have replace, replaceAll, and replaceFirst methods that will do this automatically for you. I will not give you credit if you use these methods on this assignment. These methods work, but are not required for the AP exam. Use them in the future, but you need to learn the algorithm for replacing text using the indexOf and substring methods. 

 

Resources

Java Textbook

  • Chapter 3: Using Classes and Objects
  • Chapter 5: Conditionals and Loops
  • Chapter 6: More Conditionals and Loops

Bill Barnum's AP Computer Science A videos

AP College Board test prep videos