Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Add white spices before the String element

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Add white spices before the String element

    This will bring to mind the typical create a star triangle with the right angle on the right, whereby each line has a number of white spaces preceding the *, ie
    *
    **
    ***
    ****
    and so forth.

    In brief I have an ArrayList, whereby each element is of type String. So that in the first element: I may have public class hi{, second: public void run(){, third: if (this) return that, and fourth: public void eat(){, etc...

    I have created a method which traverses each String element in search of the "{" (which it has trimmed of all the preceding white spaces to make the indentation easy). On finding this {, the program should insert a x number of whitespaces before depending on the order in which the "{" appears in the array. For instance, referring back to my example, on finding the first bracket, the first element is left as it is no white space is inserted before the element. Yet, on find the second bracket, I want that element to be indented a number of white spaces (or pixels if you suggest I use HTML tags here). The on finding the third, the element is indented a bit further and so forth.

    How do you suggest I implement this? I was thinking of getting the element index so that if i = 1, white spaces = i - 1 (number of times some while loops in my class will be executed to output that whitespace). Likewise, if i = 2, white space inserted = 1 and so forth...This may work, but it is not particularly interesting...

    Many thanks!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Add white spices before the String element

    Using an index should work quite well. Increment it upon finding '{', and decrement it upon finding '}', using its value in a loop to insert the spaces/tabs. Sounds interesting to me. If you post the beginning portions of an implementation it might be easier for others to lend a hand.

Similar Threads

  1. [SOLVED] theory behind testing each element of an array.
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: February 5th, 2010, 09:04 AM
  2. How to extract a particular element details which has more references ???
    By j_kathiresan in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 31st, 2009, 01:11 AM
  3. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM
  4. How can i point the mouse over a html element within the web browser?
    By bobomonkey in forum Java Theory & Questions
    Replies: 2
    Last Post: October 18th, 2009, 12:37 PM
  5. Replies: 4
    Last Post: June 18th, 2009, 09:23 AM