A school assignment has tasked me with replacing all instances of one String (in this case "_") with alternating Strings (in this case, alternating between < I > and < /I >).
NOTE: The method immediately returns the original String if there is an odd number of "_"s.
Here is what I have tried already, it always prints out the original string:
public String convertItalics ()
{
String temp = line;
if(temp.length() - temp.replace("_", "").length() % 2 == 0)
{
temp = line;
while(temp.contains("_"))
{
temp.replace("_", "<I>");
temp.replace("_", "</I>");
}
}
return temp;
}
Any help that you could give would be greatly appreciated.