I'm trying to replace add " and cats"
just after "dogs"
but I can't seem to get the right output with only the "s"
from the "dogs"
left out everytime.
class Main {
public static void main(String[] args)
{
String message = "I love dogs! I have a dog named Coco. My dog's very smart!";
int i = 0;
while(message.indexOf("dogs") >= 0)
{
i = message.indexOf("dogs");
String sub1 = message.substring(0 , i + 3);
String sub2 = message.substring(i + 4);
message = sub1 + " and cats" + sub2;
}
System.out.println(message);
}
}
Right now my output is "I love dog and cats! I have a dog named Coco. My dog's very smart!"
. But the "s"
is missing and I can't figure out how to fix that.