• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Indenting output streams

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Darin Niard
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well thank you, you now see why I post in the beginner forum!!
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic