Mutate is spelled without the double tt
. Your code will never mutate the last position because of the way you call random.
The problem is substring(start, end)
returns a string that does not include the end index character. So you're losing one character. The whole if block is unneeded as well.
If you're trying to mutate, you could write a function like this:
public void mutate() {
Random rand = new Random();
int mutPos = rand.nextInt(valueString.length());
valueString = valueString.substring(0, mutPos)
+ rand.nextInt(10) + valueString.substring(mutPos+1);
}
Here's some tips:
Given string "ABC"
.
substring(1,2)
returns "B"
, index 1 inclusive to index 2 exclusive.
substring(0, string.length())
return whole string.
substring(0)
returns whole string.
substring(i, i)
returns ""
Substring also returns ""
if it starts (and ends) at index one past the last character (at string.length()
index.
This allows you to easily handle corner cases without if sentences as demonstrated in the code I wrote above.