2

I am trying to replace the last char in my String .

String bowlNumber = 1.1;

bowlNumber.replace(bowlNumber.charAt(bowlNumber.length-1), 2);

By this I am able to replace my last character i.e 1 to 2.. but the problem is as my first character is also 1 , it also changes that to 2.

What i want is to change 1.1 to 1.2 , but it makes 1.1 to 2.2

Any idea?

1

3 Answers 3

6
bowlNumber = bowlnNumber.substring(0,bowlNumber.length()-1) + "2";
Sign up to request clarification or add additional context in comments.

3 Comments

Should be bowlNumber.length() as length is a function.
Wrong output. It will print only 12 not 1.2.
true ... true ... i wrote it in a hurry just as an ideea... i think i've corrected now...
3
String bowlNumber="1.1";
String replaceEnd = bowlNumber.replaceAll(".$", "2");
System.out.println(replaceEnd);

The OutPut is: 1.2

Comments

3

Try following code:

    String bowlNumber = "1.1";
    bowlNumber=bowlNumber.substring(0, bowlNumber.length()-1)+"2";
    System.out.println(bowlNumber);

Output

1.2

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.