- Write a file named TextProcessing.java that will replace all occurrences of a word in a string with another word. You file should ask user to enter a sentence, then ask the user to enter the two strings where the first one is the word you like to replace and the second is the word you like to replace with. At the end, you file should print out the sentence with the strings replaced.
Please pay attention the String method replace() will replace all the occurrences, however, we require to replace the exact word only. Therefore, you need carefully check the words before your replace them.
As an example, given the sentence “The ants are crossing the road”, your method should be able to replace the word “ants” with another word, like “pigs.”
But if the sentence is:
“The ants are crawling down the pants”, your method should replace the word “ants” with another word, but it should NOT replace the word “ants” in the word “pants.”
Write pseudocode to show your programming logic before you start coding.
Test your file with sentence “The ants are crawling down the pants”, and you need replace ‘ants’ with ‘bugs’.
here is the code that i tried
import java.util.Scanner;
public class text {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.print("Enter a sentence ");
String str1= sc.nextLine();
Scanner sc1= new Scanner(System.in);
System.out.print("Enter a repleacemnet ");
String str2= sc1.nextLine();
Scanner sc2= new Scanner(System.in);
System.out.print("Enter a word to repleace ");
String str3= sc2.nextLine();
str4=str1.repleace(str2,str3);
System.out.print(str4);
}
}
It turns out error:
text.java:21: error: cannot find symbol
str4=str1.repleace(str2,str3);
^
symbol: variable str4
location: class text
text.java:21: error: cannot find symbol
str4=str1.repleace(str2,str3);
^
symbol: method repleace(String,String)
location: variable str1 of type String
text.java:23: error: cannot find symbol
System.out.print(str4);
^
symbol: variable str4
location: class text
3 errors