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,

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.