0

This bit of code im working on is supposed to print the inputted string in steps minus 1 letter every time.

ex.
BANANA
BANAN
BANA
BAN
BA
B

Here is my code:

Triangle triangle = new Triangle(); //test driver
    triangle.triangleLoop("BANANA");    //test driver

    public void triangleRecursive(String input){
    	String recursiveInput = "";
		int strLength = input.length();
		String[] temp = input.split("");
		for(int i=0;i<strLength;i++){
			System.out.println(input);
			temp[strLength - i] = "";
			for(int c=0;c<temp.length;c++){
				recursiveInput = recursiveInput + temp[c];
			}
			triangleRecursive(recursiveInput);

		}
    }

My code works fine and prints the desired output. However what happens afterwords is a little bit peculiar.

ex.
//desired output here, No Problems! then....
BA
B
BAN
BAB

It prints this pattern infinitely. Ive been trying to fix it but I havent found a way that won't break the part that works. Any help is appreciated.

Cheers,

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Ezzaral
0

Why not use substring() instead of the loop? It reduces the whole method to about 5 lines.

You still need to reduce the string each invocation until you reach a terminating condition. Your current code has no termination condition and hence the infinite loop.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.