java - How to increment the number in a String by 1? - Stack Overflow
Increment the number by 1 whenever i call the funtion in java - Stack Overflow
Is it possible to increment all array elements by some amount at once?
Question about incrementation when looping.
Videos
Just to preface, this isnt homework - Im self learning. Ive used Stack overflow but I dont think im searching the right thing, so I cant find what Im looking for
For my study book (Learn Java the Hard Way) , i'm on an exercise where I have to increase
i = 5;
to 10 by only using ++ , i know I could do
i++; on repeated lines but my study drill says I can do it on one. Which I would like to figure out
"Add code below the other Study Drill that resets iโs value to 5, then using only ++, change iโs value to 10 and display it again. You may change the value using several lines of code or with just one line if you can figure it out. "
Perhaps ive read it wrong but I cant find a way of using only ++ to do this in one line.
Ty for the help :)
If you want to parse the number after the v as an int and increment it by 1, just do so by substringing, adding 1 to the number and concatenating the resulting incremented number back into a string with "v" prepended:
String version = "v1";
String newVersion = "v" + (Integer.parseInt(version.substring(1,version.length()))+1);
System.out.println(newVersion);
Alternatively, you could keep track of the current version in an int, increment that and construct your new String by appending that int to a "v", without having to call substring().
I will suggest you to store the version in an int. so the codes will be following
int versionNumber = 1;
whenever you want to increment just increment it using
versionNumber++;
and to print or store as a complete version do following
String version = "v" + versionNumber;