-1
public class MainWindow {
    public static void main(String[] args) {
        String s = "88,24";

        if(s.contains(",")){
            s.replaceAll(",", ".");
            System.out.println(s);
        }
    }
}

For the new application I'm working on I have to be able to replace a , with a . but I've had no success yet. Does anyone know of a way to do this?

GhostCat
  • 137,827
  • 25
  • 176
  • 248

2 Answers2

4

Strings are immutable. Their content can not be changed upon creation.

Therefore any method that changes the content of a String object will return that changed value to the Caller. No matter if you replace, concat, ...

Therefore you need

s = s.replace ...

to have the reference s point to that updated string value.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
1

ReplaceAll is not inplace in nature. So,you have to explicitly assign it to s. Here is the code.

public class MainWindow {
    public static void main(String[] args) {
        String s = "88,24";

        if(s.contains(",")){
            s = s.replaceAll(",", ".");
            System.out.println(s);
        }
    }
}
bigbounty
  • 16,526
  • 5
  • 37
  • 65