I am trying to stop this recursive loop in Java.
I have made a program which prints numbers like Output :1 2 3 4 5 6 5 4 3 2 1 0 1 2 3 4 5 6 5 4 3 2 1
It's like creating a wave of increasing and decreasing numbers. But i want to stop, when it gets more than the length of the string. Can anyone give an alternate way.
boolean isboolean = true;
public void recursive(int data, String s) {
int counter = 0;
Loop1:
while (isboolean) {
counter++;
data = data + 1;
System.out.print(data + " ");
if (data > 5) {
isboolean = false;
}
if (counter > s.length()) break Loop1;
}
Loop2:
while (!isboolean) {
data = data - 1;
System.out.print(data + " ");
if (data == 0) {
isboolean = true;
}
}
recursive(data, s);
}
I want to stop this wave of numbers when it gets more than String s length. And prints output: 1 2 3 4 5 6 5 4 3 2 1 0 1 2 3 4 5 6 5 4 3 2 1 but stops, when it's more than string length.
isbooleanand where does it get defined?void?if (data>s.length()) return;Place it as the first line in the add method and it will break.