Compare value at each position of the String in Java?

3

I'm having trouble comparing a value at each position of a String , follow the example below:

parametro = "30,60,90";
private int countVirgulas(String parametro) {
    int count = 0;
    for (int i = 0; i <= parametro.length(); i++) {
        if (parametro.substring(i).equals(",")) {
            count += count;
        }
    }
    return count;
}

For example I have a String with the value "30.60.90".

I would like to compare in each position of it and where there is a comma, and increase in count .

In the above example does count return me 2?

    
asked by anonymous 30.12.2015 / 11:23

2 answers

4

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 .

    
30.12.2015 / 11:43
1

Although the question has already been answered, I would like to give you another way to implement the comma counter using an interesting method of the String class as a way for you to gain more visibility than this class can offer you.

public class ContadorDeVirgulas {

    public static void main(String[] args) {
        System.out.println(contadorDeVirgulas("30,60,90"));
    }

    public static int contadorDeVirgulas (String parametroComVirgula) {
        String[] parametroSeparado;
        int count = 0;

        //método que separa as strings de acordo com padrão que vc deseja, no seu caso, a vírgula
        parametroSeparado = parametroComVirgula.split(",", 100);

        //parametroSeparado.length retorna o tamanho do array de String após a remoção das vírgulas
        //o -1 foi usado para retirar da contagem o primeiro valor (30), pois não há vírgula antes dele
        count = parametroSeparado.length - 1;

        //retorno do contador
        return count;
    }

}

In this implementation I used the split method of the String class. This method splits a string into parts according to a regex ( regular expression) .

The code is commented on, so see it working, see Ideone .

For more references on this methods, here are some readings that I recommend:

Java - String split () Method Source: tuturialspoint

Java documentation on the split method Source: Oracle Docs

Regex Documentation Source: Oracle Docs

    
30.12.2015 / 12:56