Issue
I am trying to create an Android calculator which uses strings in Kotlin. I do not know how to delete a comma (or the negative) if it already contains one.
Here is my code which adds the comma correctly but doesn't delete it if the user clicks again:
class="lang-kotlin prettyprint-override">if (!buClickValue.contains(".")) {
buClickValue += "."
} else {
buClickValue.replace(".", "")
}
Solution
The replace()
method is designed to return the value of the new String
after replacing the characters. In your case, the value obtained after replacing the characters is never reassigned back to the original variable.
Specifically in your else clause, the line should be changed to -
buClickValue = buClickValue.replace(".", "")
Answered By - Ameya Pandilwar
Answer Checked By - Candace Johnson (JavaFixing Volunteer)