Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Indenting output streams

 
Bix Beiderbecke
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say I have the following class:
class Adder
{
private boolean flag = false;
private int indent = 4;

public int sumSquares(int n)
{
return sumSquares(n, 0);
}

private int sumSquares(int n, int count)
{
int temp = 0;
if(flag)
System.out.println("entering sumSquares("+n+")");
if(n == 0)
{
if(flag)
System.out.println("exiting sumSquares(0)=0");
return 0;
}
else
{

temp = sumSquares(n-1, count++)+n*n;
if(flag)
System.out.println("exiting sumSquares("+n+")="+temp);
return temp;
}
}

public void setDebug(boolean value)
{

flag = value;
}
}
now I want to indent 4 spaces for each level of recursion the output lines
so the output would look like this
entering sumSqaures(5)
entering sunSquares(4)
entering sumsqaures(3)
entering sumSquares(2)

any ideas?
 
Bix Beiderbecke
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry it didn't show the proper output: each entering sumSquares line should be indented 4 spaces the second line, 8 spaces the third line, and 12 spaces the fourth line
 
Darin Niard
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Darin Niard
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To answer your question...

Just write a for loop that uses your 'count' variable as a stopping point to append spaces to a string, then stick that string on the beginning of your print statements. You need to pass ++count or count+1 instead of count++ though, because right now your program is just passing 0s in each call.
[ July 31, 2004: Message edited by: Darin Niard ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like your apparent taste in music, "Bix", but I think you need to take a look at our display name policy and please edit your display name to follow that policy. Thanks.
[ July 31, 2004: Message edited by: Jim Yingst ]
 
Bix Beiderbecke
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks that makes since, and if I knew what code tags were I would use them. As for the name, that is my real true first name, no joke. Bix "Bismark" Beiderbecke is my great great uncle, I apologize for any confusement.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you post next time, look under the edit box. Just below the "Add Reply" button you see "smilies" and Instant UBB Code buttons. One of the buttons is a "code" tag which puts [ code ] tags into your post. If you put your own code between those tags, the formatting of that code is preserved, usually making it easier to read.
 
Bix Beiderbecke
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well thank you, you now see why I post in the beginner forum!!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bix "Bismark" Beiderbecke is my great great uncle

No $#!%?!!! Cool. Carry on then. I will replay a compilation of your great great uncle's work, in his honor. Cheers...
[ July 31, 2004: Message edited by: Jim Yingst ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic