whats wrong? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

whats wrong?

import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scan = new Scanner (System.in); String x = scan.nextLine(); x.replace ("o" , "p"); System.out.println (x); } }

7th May 2020, 6:27 PM
Yahel
Yahel - avatar
3 Answers
+ 4
replace doesn't change the original string, it returns a modified version of that string. x = x.replace("o","p"); should work.
7th May 2020, 6:30 PM
Kevin ★
+ 1
String are immutable.. Read about that topic.. And if you need replaced result.. You need to save result by replace like x=x.replace("o","p"); This only replace first occurrence of "o", For all, use x=x. replaceAll("o", "p") ;
7th May 2020, 6:33 PM
Jayakrishna 🇮🇳
0
Kevin Star thanks!
7th May 2020, 6:32 PM
Yahel
Yahel - avatar