-1

The program should allow a user to enter a string, a substring they wish to find and another string with which they wish to replace the found substring.

The output of your program should be similar to the output given below:

Please enter a string: Hello world

Please enter the substring you wish to find: llo

Please enter a string to replace the given substring: @@

Your new string is: he@@ world

I am new to Java and cant find and so far this is what I have:

import java.util.Scanner;

public class searchReplace 
{
    static String word, substring, newWord;
    static String output = "";
    

    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);
        
        System.out.println("Please enter a string: ");
        word = input.next();
        
        System.out.println("Please enter the substring you wish to find: ");
        substring = input.next();
        
        System.out.println("Please enter a string to replace the given substring: ");
        newWord = input.next();
        
        replace(substring,newWord);
        
        
        
        input.close();


    }


    private static void replace(String substring, String newWord) 
    {
        if(output.contains(newWord))
        {
            System.out.println(output);
        }
        
        else
        {
            output = word.replaceAll("substring","newWord");
            replace(substring,newWord);
        }
        
    }

}

I Get The Error:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.contains(java.lang.CharSequence)" because "searchReplace.output" is null at searchReplace.replace(searchReplace.java:33) at searchReplace.main(searchReplace.java:21)

2 Answers2

0

For your goal, you need to use just:

output = word.replace(substring, newWord);

instead of:

word.replaceAll("substring", "newWord");

You don't need to make any recursion. Replace function will replace your substring inside word with the newWord for each occurence.

  • Thank you, question requires me to use recursion. I see my error now with the " ". – ataaullahk Apr 08 '21 at 07:36
  • If you want to use recursion, you need to completely change the approach. First of all you cannot use the replace function from the Java library. You must apply Dividi et impera approach. So you have identify a base case such as: - the word is empty or is shorter than substring ==> return original word - word is as long as the substring ==> if word == substring: output = substring else return word - word is longer than substring ==> split the word in blocks which length is the same as substring and call back replace – carminoplata Apr 08 '21 at 17:58
  • I hope it's clear. If you need, I'll add the code but you need to understand how recursion works, it is not a Java problem – carminoplata Apr 08 '21 at 18:00
  • Thank you. I have changed it to: if( ! output.isEmpty()) – ataaullahk Apr 08 '21 at 18:08
0

Recursion is not needed:

public static void main(String [] args) {
    try (Scanner scanner = new Scanner(System.in)) {
        System.out.println("Enter String: ");
        String input = scanner.nextLine();
        System.out.println("String to replace: ");
        String toReplace = scanner.nextLine();
        System.out.println("Replace with: ");
        String replaceWith = scanner.nextLine();
        
        System.out.println(input.replaceAll(toReplace, replaceWith));
    }
}
Ryan
  • 1,762
  • 6
  • 11