aspose file tools*
The moose likes Beginning Java and the fly likes Indenting output streams Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Indenting output streams" Watch "Indenting output streams" New topic
Author

Indenting output streams

Bix Beiderbecke
Greenhorn

Joined: Jun 28, 2004
Posts: 15
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

Joined: Jun 28, 2004
Posts: 15
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

Joined: Jun 08, 2004
Posts: 118
Darin Niard
Ranch Hand

Joined: Jun 08, 2004
Posts: 118
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

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
Bix Beiderbecke
Greenhorn

Joined: Jun 28, 2004
Posts: 15
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

Joined: Jul 22, 2000
Posts: 9043
    
  10
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.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Bix Beiderbecke
Greenhorn

Joined: Jun 28, 2004
Posts: 15
Well thank you, you now see why I post in the beginner forum!!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Indenting output streams
 
Similar Threads
Help with reprinting downloaded text from FTP site onto GUI!
nullpointerexception
RefList help
Currency Problem + General Advice
Cannot find symbol error in switch-case