0

I have the following method and I wanted to change the for each loops to normal for loops. So I tried something like this

for (int i = 0; i < above.length(); i++) {
    char x = above.toCharArray();
    if (x == 'x') {
        counter++;
    }
}

But I know that's wrong. So, what's the right way to change these for each loops to normal for loops?

public static int neighbourconditions(String above, String same, String below){
    int counter = 0;
    if(above != null){
        for(char x : above.toCharArray()){
            if(x == 'x'){
                counter++;
            }
        }
    }
    for (char x : same.toCharArray()){
        if (x == 'x'){
            counter++;
        }
    }
    if (below != null){
        for(char x : below.toCharArray()){
            if (x == 'x'){
                counter++;
            }
        }
    }
    return counter;
}
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
JavaJoker204
  • 83
  • 1
  • 9
  • ***char x = above.toCharArray();*** that makes no sense and doesnt compile... – ΦXocę 웃 Пepeúpa ツ Sep 12 '17 at 14:37
  • 1
    if you are using eclipse you can click on the 'for' and then Ctrl+1 which shows a menu where you can convert between the different types of for loops (Ctrl+1 works for other useful stuff too (e.g. convert switch-case to if) – JavaMan Sep 12 '17 at 14:50
  • 1
    I found this question https://stackoverflow.com/questions/28259009/how-to-automatically-convert-if-else-if-statement-to-switch maybe Alt+Enter also works for converting for loops – JavaMan Sep 12 '17 at 14:54

4 Answers4

5

Just use a basic for loop whose bounds are governed by the length of the string, which is the same as the size of the corresponding character array, e.g. the first loop:

for (char x : above.toCharArray()) {
    if (x == 'x') {
        counter++;
    }
}

would become this:

for (int x=0; x < above.length(); ++x) {
    if (above.charAt(x) == 'x') {
        counter++;
    }
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • I think there's no difference here if I write ++counter. Or should it really be ++counter? – JavaJoker204 Sep 12 '17 at 14:51
  • 2
    @JavaJoker204 The isolated statements `counter++` and `++counter` are functionally identical, though they are different. If we were to assign a variable to `counter++`, and `counter=3`, then the variable would be assigned to 3, and then the counter would be incremented. If, on the other hand, we assigned a variable to `++counter`, and `counter=3`, then the variable would be assigned to 4. – Tim Biegeleisen Sep 12 '17 at 14:53
  • 1
    @JavaJoker204 in my mind it should be only for fashion along with `++x`. In your code returning `counter` it's only a matter of preference – fantaghirocco Sep 12 '17 at 15:01
  • My buddy told me that ++counter is faster in C++. I didn't believe him, but then I decompiled a test program (Visual Studio), and ++counter did use fewer instructions! – Jamie Sep 12 '17 at 15:48
  • @Jamie see https://stackoverflow.com/a/39306/1575188 and https://stackoverflow.com/a/4261743/1575188 if you like – fantaghirocco Sep 12 '17 at 16:45
2

You can use above.toCharArray().length and to get the value above.toCharArray()[i] and with collaboration with @chillworld:

You can create an array of char[] array = above.toCharArray(); outside the loop like this :

char[] array = above.toCharArray();
for (int i = 0; i < array.length; i++) {
    if (array[i] == 'x') {
        counter++;
    }
}
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
1

I would imagine that you would have to find the length of each string being passed to the method. Example if the content of above is equal to "some_randome_string" you would have to find the length of the string or how many characters are in the string. in this case that would be 19. then you would do;

        if <variable != null > {
           for (x = 0; x < 18 ; x++) {
       code block to be executed
      } 
    }
omarioja
  • 11
  • 1
1

If you need to count occurances of character x then you may try below or a regex:

String upAbove = above.replaceAll("x","");
int count = above.length() - upAbove.length();
Amit Mahajan
  • 895
  • 6
  • 34