Results 1 to 5 of 5
  1. #1
    Cylab is offline Member
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default How to increase whitespace?

    Depends on the size of stack, I need to add whitespace.
    stack: [a,b,c,d,]

    wanted Ouput:
    a
    " " b
    " "," "c
    " "," "," "d

    Please help, how can I do it.

    Sincerely appreciation for any suggestion.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,772
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Cylab View Post
    Depends on the size of stack, I need to add whitespace.
    stack: [a,b,c,d,]

    wanted Ouput:
    a
    " " b
    " "," "c
    " "," "," "d

    Please help, how can I do it.

    Sincerely appreciation for any suggestion.
    Here's a clue: a next line has one space more than a previous line before the stack element is printed. StringBuilders are ideal for this purpose.

    kind regards,

    Jos

  3. #3
    Cylab is offline Member
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    The code works to put one more space than a previous line.

    import java.util.*;

    public class Sbuilder
    {
    public static void main(String[] args)
    {

    Stack<String> stack = new Stack<String>();
    String[] s ={"a","b","c","d","f"};

    StringBuilder st = new StringBuilder();

    for(int i=0;i<s.length;i++){

    // System.out.print(st.insert(i, " "));
    System.out.print(st.append(" ")+stack.push(s[i]));

    System.out.println();
    }
    System.out.println(st);
    }
    }

    However, how about for following result.

    wanted Ouput:
    a
    " " b
    " ",c
    d
    " " b
    How it works? Any hint please???????????????????

  4. #4
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    195
    Rep Power
    5

    Default

    System.out.println("\t"+stack.push(s[i]));
    System.out.printf(" "+stack.push(s[i]));
    Last edited by Sno; 08-09-2010 at 08:29 PM.
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,772
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Sno View Post
    System.out.println("\t"+stack.push(s[i]));
    System.out.printf(" "+stack.push(s[i]));
    Cool, you try that when an s[i] equals "%d" or similar.

    kind regards,

    Jos

Similar Threads

  1. Replies: 2
    Last Post: 03-31-2010, 01:26 AM
  2. How increase dynamic array?
    By artemff in forum New To Java
    Replies: 5
    Last Post: 01-01-2010, 01:22 AM
  3. removing duplicate whitespace
    By loki in forum New To Java
    Replies: 1
    Last Post: 04-25-2009, 06:54 PM
  4. why the number will not increase?
    By rayda in forum New To Java
    Replies: 3
    Last Post: 03-16-2009, 06:47 AM
  5. Reproducing whitespace with DOM
    By doofuslarge in forum XML
    Replies: 1
    Last Post: 02-11-2009, 02:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •