First of all, you have to read the whole line with nextLine()
, because next()
only reads the next token.
Also, if you want to modify the original string, you have to assign the result of replace()
to str1
:
str1 = str1.replace("replace", "Hi");
Code:
Scanner cumle = new Scanner(System.in);
System.out.println("Enter the sentence u want to replace :");
String str1 = cumle.nextLine();
if (str1.contains("replace")) {
str1 = str1.replace("replace", "Hi");
System.out.println("Replaced Sentence: " + str1);
} else {
System.out.println("Sentence doesn't contains that...");
}