I'm fairly new to programming and I started to write some basic codes. I tried to create an algorithm that found every position of the character you wanted in a string. If I were to enter "helllo" as a string and wanted to search for the character l, it would output 2, 3 and 4. The code worked fine until I tried to trick the code a bit to change something. To do so, I first removed the termination condition in the "for loop" and instead added an if statement during the loop to break it. I spent about 2h on the error that occurred after and I still can't find out what's happening.
Here's the code and the output it gives me. (I know my code is a mess and not properly optimized, but right now I would just like to know what is happening. I'll rearrange it later if I get it to work. thank you^-^.)
When I run the code, instead of displaying as it should, "7, 8, 9, 10"(it searches for the character ^ in the string Oioriew^^^^) it outputs "7-1,8,9,10". To fix it, I can simply insert the termination condition in the loop again, which was, "pow != -1" but at this point, I really want to know why it happens.
public class Tests {
static void zeMethod(String mainString,char charToFind) { 
        int  a = 0;
        String b, c;
        char chartToConvert;
        c = ""; 
        b = "";
        for (int pow = mainString.indexOf(charToFind);
        ; // *the condition was here.*
        pow = mainString.indexOf(charToFind, pow + 1)) {
        a++;
        if (a == 1){
            System.out.println("String: "+mainString);
            System.out.println("il y a un "+charToFind+" à la/aux position(s)");
        }
        if (a == 1){
            System.out.print(pow);
        }
        if (a%2 == 0 && pow != -1) {                
            c = b+", "+pow;
        }
        if (a%2 != 0 && a != 1 && pow != -1) {
            b = c+", "+pow;
        }           
        if (pow == -1){
            System.out.print(pow);
            break;
        }   
        //*end of loop*     
        }
        if (a%2 != 0){
            System.out.println(c);
        }
        else {
            System.out.println(b);
        }
}       
public static void main(String[] args){
    String string = "Oioriew^^^^";
    char chara = '^';
    zeMethod(string, chara);
}
}
I'm sorry if my question is a bit incoherent or not properly asked. This is my first time on the site and English isn't my mother language. Thank you for your time!
Edit:
I know the question wasn't clear at first, but what I meant is, why does pow become -1 after the second iteration of the loop. Also, why does the break after the System.out.print(pow); doesn't make it leave the loop. (I'm looking how to make a debugger work atm too.)
 
    