How to print every third character of a string? java

blackdiamondx :

I am trying to print every 3rd character of a string so an example would be: 123456 returns 36

But my code below returns 14

public String getEveryThird() {
    String newString = "";

    for (int i = 0; i < string.length(); i++) {

        if (i % 3 == 0) {
            newString += (string.charAt(i));
        }
    }

    return newString;
}
Uladzislau Kaminski :

Good try. The only problem is you choose the wrong remainder of the division since elements start from 0.

Try this condition:

if (i % 3 == 2)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=4353&siteId=1