-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
  • 133,361
  • 24
  • 165
  • 234

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
  • 133,361
  • 24
  • 165
  • 234
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
  • 14,834
  • 4
  • 27
  • 58