Home > Blockchain >  How to increment a number in a String in Java?
How to increment a number in a String in Java?

Time:09-22

I want to increment a number in a string in Java 8. Like this:

String lifes = "3";
lifes--

But i know that way is impossible, does you know other way to do it?

CodePudding user response:

Integer.parseInt("3")   1

The above statement results 4 which means we are parsing the String value as Integer and then incrementing in here, hope it helps!

CodePudding user response:

If the string only contains the number, I'd do something like this -

lifes = String.valueOf(Integer.parseInt(lifes) - 1);
  •  Tags:  
  • java
  • Related