0

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.)

Bouch
  • 13
  • 4
  • 1
    That is not a "restriction", it is the [loop termination condition](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html). `for (initialization; termination; increment) { statement(s) }` – Elliott Frisch Oct 05 '19 at 03:21
  • You're correct, I'll correct it. – Bouch Oct 05 '19 at 03:25
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) – Andreas Oct 05 '19 at 03:29
  • Looks good, I'll look into it, thank you. – Bouch Oct 05 '19 at 03:33
  • Move the `if` with `break` to the **first** statement in the `for` loop body (not the last). The order matters. Before the second iteration of the loop, the "increment" occurs. Then the termination is tested. Then the statements are executed. You currently have the termination after the statements. You want `for (initialization; ;increment) { termination; statement(s); }` **not** `for (initialization; ;increment) { statement(s); termination; }` – Elliott Frisch Oct 05 '19 at 03:33

2 Answers2

0

[...] instead of displaying as it should, "7, 8, 9, 10" [...] it outputs "7-1,8,9,10 [...] I really want to know why it happens

Sounds like you're asking why it prints -1, which happens because you explicitly asked it to:

if (pow == -1){
    System.out.print(pow);
    break;
}

If pow is -1 then print pow (aka -1) and exit the loop.


UPDATE

As for why the order of the output, use a debugger and step through the code one statement at a time, and you'll see.

But, lets see: We can agree that loop will iterate with pow having these numbers in this order: 7, 8, 9, 10, -1

Why does it print 7 first?
Because you have this code inside the loop:

a++;
if (a == 1){
    System.out.print(pow);
}

Why does it print -1 next?
Because there are no other print statements inside the loop, which would be a lot more apparent if you indented the code correctly, i.e. indented the content of the loop.

Why does it print 8,9,10 last?
Because you print the content of b or c after the loop, and that is the content of whichever one of them is being printed.

Note that first value (-7) is not added to b, because you explicitly exclude it in the if statement.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • My mistake, what I meant is, why does the code changes pow to 7, then to -1, then to 8. Instead of going 7,8,9... I know a -1 means character not found, but I don't know why it does it. – Bouch Oct 05 '19 at 03:37
  • And even though I explicitly ask to break from the loop after printing pow, it still goes on to 10 and then exits the loop. – Bouch Oct 05 '19 at 03:39
0

I'm all good, thank you all for your time and Andreas, for the debugger option. It's a must-have for anyone programming. I don't know how I didn't see this earlier, but to break, it had to become -1 and since I had to break before I could print my answer, -1 was first.

Bouch
  • 13
  • 4