0

enter image description here

  1. 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
Needhelp
  • 11
  • 4

1 Answers1

0

At first, making a new scanner object every time input is required, is not necessary. After all the necessary input is acquired, this code splits the sentence into a string array of words in the stream, and then each word is either replaced, if it is equal to str3, or is returned as-is. Finally the results of the stream are joined into a string, which is saved in str1, and then printed.

import java.util.*;
import java.util.stream.Collectors;

class text {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a sentence ");
        String str1 = sc.nextLine();

        System.out.print("Enter a replacement ");
        String str2 = sc.nextLine();

        System.out.print("Enter a word to replace ");
        String str3 = sc.nextLine();

        str1 = Arrays.stream(str1.split(" "))
                     .map(s -> {
                         if (s.equals(str3))
                            return s.replace(str3, str2);
                         return s;
                     }).collect(Collectors.joining(" "));

        System.out.print(str1);
    }
}

Output:

Enter a sentence The ants are crawling down the pants
Enter a replacement bugs
Enter a word to replace ants
The bugs are crawling down the pants
solid.py
  • 2,782
  • 5
  • 23
  • 30
  • 1
    Re-using a variable for a different purpose than the one it's been declared for just because it's no longer used and has the correct type is not, IMHO, a good suggestion. – Federico klez Culloca Oct 07 '20 at 08:56