I’m just stuck with a problem (maybe Simple). But I can’t figure out how to solve it, maybe someone of you can help me.
I receive as input an string with this format:
D0001001.tiff
And I need to return the next one (given that the next one is the received incremente by factor of one.
Input: D0001001.tiff
Output: D0001002.tiff
No zero can be missed. The method I have is this (without refactoring 😉 )
private String getNextImageName(String last_image_name)
{
// Splits the name from the start to the . (not inclusive)
String next_name = last_image_name.substring(0, last_image_name.indexOf(".") - 1 );
String next_extension = last_image_name.substring(last_image_name.indexOf(".") + 1, last_image_name.length() - 1 );
String next_name_without_D = next_name.substring(1);
int next_name_withoud_D_value = Integer.parseInt( next_name_without_D );
// Increments to get the new name
next_name_withoud_D_value++;
String full_next_name = "D" + next_name_withoud_D_value + "." + next_extension;
return full_next_name;
}
But the results are not as the expected:
Input: D0002001.tiff
Output: D201.tif
—
There are some constraints, for example, the number of 0 can’t disappear because eventually the file can hace different number:
D0001001.tiff
or
D9999999001.tiff
but the second one goes only through 999
D0001001.tiff
to
D0001999.tiff
By this moment I’m so stuck that I can’t even think…
Thanks
If would use Regexp instead to make the code a bit cleaner:
A couple of comments:
Lookup Regexp specification to understand patterns and captures. The given pattern basically “returns” the numberical value and the extension
String.format()can format numbers and inserts0as pad as well.String.format("%02d, 1)returns"01"whileString.format(%02d, 200)returns"200".