The main problem seems to be the increment that is not occurring. At the moment you are adding to count
the very count
that initially is 0, so it will never leave this value. The auto-addition operator must be used with values that are not neutral, otherwise the value will not be changed. Even if it started with 1, the count would progress geometrically, which is not what is desired, each step should always add 1 and not the last count. It is simpler than this, just use the increment operator that is equivalent to doing count += 1
.
Another change that I would make for performance issues is to get the character instead of substring :
private int countVirgulas(String parametro) {
int count = 0;
for (int i = 0; i < parametro.length(); i++) {
if (parametro.charAt(i) == ',') {
count++;
}
}
return count;
}
See working on ideone .
Or if you prefer:
private int countVirgulas(String parametro) {
int count = 0;
for (char caractere : parametro.toCharArray()) {
if (caractere == ',') {
count++;
}
}
return count;
}
See working on ideone .
If you want to use substring
you have to hit an error, since it is not picking up one character at a time:
private int countVirgulas(String parametro) {
int count = 0;
for (int i = 0; i < parametro.length(); i++) {
if (parametro.substring(i, i + 1).equals(",")) {
count++;
}
}
return count;
}
See running on ideone .