problem with recursive method
Hello, I am trying to write a recursive method which gives this as an output(minus the stars, it won't show spaces otherwise...):
*########
* #######
* ######
*#####
*####
*###
*##
*#
now I realize my code is not currently set to write the first line but I do not understand why the rest will not work. Here is my method thus far:
publicstaticvoid pattern2(String s2)
{
if (s2.length() > 0)
{
StringBuffer b =new StringBuffer(s2);
b.reverse();
s2 = b.toString();
s2 = s2.concat(" ");
b.reverse();
s2 = b.toString();
s2 = s2.substring(0, s2.length()-1);
System.out.println(s2);
pattern2(s2);// Recursive call
}
}
Thanks for your help.
Message was edited by:
Matt_H
[1223 byte] By [
Matt_Ha] at [2007-11-26 18:42:10]

######## ####### #####################
Yeah, that way! Now how do I do that?Message was edited by: Matt_H
> Yeah, that way! Now how do I do that?> > Message was edited by: > Matt_HThe code tags preserve formatting.
Don't see what the StringBuffer stuff will achive. The first s2 = b.toString is overiden by the second, so the net effect is to reverse the stringbuffer twice, which seem unlikely to accomplish much.What actually happens?
You have to think 'recursively', i.e.
1) if your have to print n #s, print them
2) if n > 1, do it again; otherwise stop.
Here's a way to do it:void recursion(int n) {
for (int i= 0; i < n; i++) // remark 1)
System.out.println();
if (n > 1)// remark 2)
recursion(n-1);
}
kind regards,
Jos
The reverses twice because it will first add a space, reverse (puting the space on the correct side) take one of the #'s off and then after it prints it will reverse it again so that the blank space will be added to the correct position again. This is my logic anyway.
Matt, you're making things far too complicated. I don't see how the checking of the length of the String can be of use since you don't know (in the recursive method) how long the original String was, therefor, you don't know how much spaces you should add.
Try something like this:
public static void pattern(String str)
{
if( /* there are still '#' present in 'str' */ )
{
/* print str */
/* replace the first '#' with a whitespace */
/* make the recursive call */
}
}
That's all there is to it!
; )
PS have a look at the String API docs:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html (especially the indexOf(...) and replaceFirst(...) methods)
@ JosAH :I understand your method, however it doesn't do the same thing. I am trying to add spacing each time so that they form a step pattern in the opposite direction.-thanksMessage was edited by: Matt_H
:
> public static void pattern(String str)
> {
>if( /* there are still '#' present in 'str' */ )
> {
>/* print str */
> /* replace the first '#' with a whitespace */
>/* make the recursive call */
>
> }
> That's all there is to it!
> ; )
>
> PS have a look at the String API docs:
> http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Stri
> ng.html (especially the indexOf(...) and
> replaceFirst(...) methods)
Thanks a lot!
Study this approach (didn' tried but I thik it works)
public static void pattern2(String s2)
{
if (s2.length() > 0 && s2.indexOf("#") >= 0)
{
StringBuffer b = new StringBuffer(s2);
System.out.println(b.toString());
b.insert(0, " ");
b.replace(b.toString().length()-1, b.toString().length(), "");
pattern2(b.toString());
}
}
regards,
Manuel Leiria
> Study this approach (didn' tried but I thik it> works)> > ...>> regards,> > Manuel LeiriaThe length() check in your if-statement is not necessary.
> @ JosAH :
>
> I understand your method, however it doesn't do the same thing. I am
> trying to add spacing each time so that they form a step pattern in the
> opposite direction.
Well, add a second parameter 's' then, telling how many spaces you
want to print before printing #s. Then call your method recursively using
the two parameters n-1 and s+1.
kind regards,
Jos
> > ...>> Thanks a lot!You're welcome.