0

This is my code that I used for my program. I’m having difficulties in displaying it the way I want which is without the brackets and commas. I want it to be like pyramids:

    X                                              #      
   XXX                                            ###     
  XXXXX                                          #####    
 XXXXXXX                                        #######   
XXXXXXXXX                                      #########  

My code gives square brackets and commas that I don’t want. I get:

[    X    ,    XXX   ,   XXXXX  ,  XXXXXXX , XXXXXXXXX]  
[]  
[]  
[]  
[]  
[    #    ,    ###   ,   #####  ,  ####### , #########]  

My code:

Stack stackA = new Stack();   
stackA.push("    X    ");
stackA.push("   XXX   ");
stackA.push("  XXXXX  ");
stackA.push(" XXXXXXX ");
stackA.push("XXXXXXXXX");

Stack stackB = new Stack();
Stack stackC = new Stack();
Stack stackD = new Stack();
Stack stackE = new Stack();

Stack stackF = new Stack();
stackF.push("    #    ");
stackF.push("   ###   ");
stackF.push("  #####  ");
stackF.push(" ####### ");
stackF.push("#########");

Stack[] combine = new Stack[6];
combine[0] = stackA;
combine[1] = stackB;
combine[2] = stackC;
combine[3] = stackD;
combine[4] = stackE;
combine[5] = stackF;

for (int i = 0; i < combine.length; i++)
{
    System.out.print(combine[i] + "  ");
    System.out.println();
}
Lino
  • 19,604
  • 6
  • 47
  • 65
  • Can you please show us the output that your current code gives please - ahh good I see it now. - you’re essentially got an array with in an array. – Lucy Isabelle May 27 '18 at 05:38
  • @LucyIsabelle I managed to print it the pyramid way but when i move the elements from the stack and i display it, it doesnt tally and it gives me the original picture which is the pyramid photo all over again. – norainirahim May 27 '18 at 05:46
  • Are you using the `java.util.Stack` class? In that case I’m afraid that you cannot change its `toString` method. Which is what is implicitly called when you do `combine[i] + " "`. Solutions include writing your own `printStack` method and use it like `printStack(combine[i]) + " "`. Not sure at all it will get you what you really want, though. – Ole V.V. May 27 '18 at 05:46
  • Try using a nested for loop. For example for (int i = 0; i – Lucy Isabelle May 27 '18 at 05:47
  • @LucyIsabelle Okay Lucy, i'll try that. – norainirahim May 27 '18 at 05:51
  • @OleV.V. You can see my output by clicking the blue colored sentences is my description. Apparently im unable to upload a picture so stackoverflow changed the picture into link. – norainirahim May 27 '18 at 05:52
  • @OleV.V. Thank you for the edit. Im still not familiar with the formating. Also, I thought Ive already replied you just now but unfortunately my comment didnt went throught i guess so im replying now. – norainirahim May 27 '18 at 10:12
  • And had read your comment, and thanks for it, then flagged it as “no longer needed” since I didn’t think it should be left there forever. Then probably someone saw my flag and deleted your comment. Generally don’t expect comments to be persistent, they get deleted quite often if someone thinks they are chatty or no longer needed. – Ole V.V. May 27 '18 at 10:14

4 Answers4

1

Seems like your square brackets [] are from empty Stacks.

So in your for loop make sure not to print empty stacks.

To avoid the commas, don't print the stack with its automatic toString(), but iterate over it yourself.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
riorio
  • 6,500
  • 7
  • 47
  • 100
1

you need a second for loop:

for (int i = 0; i < combine.length; i++) {
    if (combine[i].size() > 0) {
        for (Object obj : combine[i]) {
            System.out.println(obj);
        }
    }
}

This will print:

    X    
   XXX   
  XXXXX  
 XXXXXXX 
XXXXXXXXX
    #    
   ###   
  #####  
 ####### 
#########
Guilherme Mussi
  • 956
  • 7
  • 14
0
    for (int line = 0; line <= 5; line++)
    {
        for (int i = 0; i < combine.length; i++) {
            Stack st = combine[i];
            if (st.size() > line) {
                System.out.print(st.get(line) + "  ");
            } else {
                System.out.print("           ");
            }
        }
        System.out.println();
    }

This prints

    X                                              #      
   XXX                                            ###     
  XXXXX                                          #####    
 XXXXXXX                                        #######   
XXXXXXXXX                                      #########  

Challenges include:

  • You can print only lines to System.out, so printing one pyramid at a time will not give you the output you wanted with the pyramids side by side. Instead on each line we need to print one element from each stack that is high enough to have an element on that line. For stacks that are not high enough I print a blank string to make sure the following stacks line up correctly.
  • I am assuming that no pyramid is higher than 5 and that all pyramid elements are exactly 9 chars wide. The code could be refined to take other sizes into account if needed.

For better use of the Java library classes you may consider the following. It’s not what you asked, and please ignore if you don’t want to use it.

  • Use generics. For example Stack<String> stackA = new Stack<>();. This will allow you to handle the elements you get from the stack as String objects rather than just Objects.
  • Since generic classes don’t always work well inside arrays, you may use a List instead of your array, for example List<Stack<String>> combine = new ArrayList<>(); (optionally give a suggested capacity: new ArrayList<>(6)).
  • The Stack class is considered legacy. The docs say that you should prefer the Deque interface. I recommend the ArrayDeque class, it implements the interface. Like:

    Deque<String> stackA = new ArrayDeque<>(5);
    
    List<Deque<String>> combine = new ArrayList<>(6);
    combine.add(stackA);
    

    To use a Deque as a stack you use its addFirst method for push and its removeFirst for pop. It’s explained in the documentation.

  • You may use the enhanced for loop, for example for (Deque<String> pyramid : combine).

PS If you wanted your stacks to grow from the bottom, you should probably push the widest element first and print the lines in the opposite order. You’ll find out soon enough.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

Maybe this code will help if you don't need the combine stack array.

    while (!stackA.empty()) {
        stackB.push(stackA.pop());
        stackC.push(stackF.pop());
    }

    while (!stackB.empty()) {
        System.out.println(stackB.pop() + "        " + stackC.pop());
    }

Output:

    X                #    
   XXX              ###   
  XXXXX            #####  
 XXXXXXX          ####### 
XXXXXXXXX        #########

The two stacks have the same size. So looping time is same. you know LIFO (last in first out) is stack basic. In the first loop I popped up two Stacks for reverse order. In the last loop I just popped and printed the stack.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Faiz Ahmed
  • 396
  • 6
  • 13
  • How does that work? Please explain. It’s a surprising solution. – Ole V.V. May 27 '18 at 06:43
  • stacks size same. So looping time is same. you know LIFO (last in first out) is stack basic. In first loop i popped up two Stack for reverse order. In last loop i just popped and printed the stack – Faiz Ahmed May 27 '18 at 07:50