For example,
How do I use this custom print method to display something in the main method?

public static void print(Stack<String> myStack ) { 
 int count = 1, sizeOfStack = myStack.size(); 
 System.out.println("\nYour stack has size: " + myStack.size()); 
 for (String myString : myStack) 
 {System.out.print(count + ". " + myString); 
 if(count!=sizeOfStack) System.out.print( " -> "); 
 else System.out.print( " :Last"); 
 count++; 

Dani AI

Generated

Short answer: build and fill a Stack<String> in main, then hand that same Stack to the print method. is right — create the stack instance and pass it to the print routine. If the print method is in the same class as main and declared static, call it directly; if it lives in a different class, call it via the class or an instance.

For the pancake loop that started, the missing statement is the stack push. Example (only the loop body shown here — the surrounding for was already posted):

pancakeStack.push(buckwheat);

That will place one buckwheat pancake on the top each iteration; run it eight times to get eight pancakes. Use a 0-based loop (for (int i = 0; i < 8; i++)) or 1-based (k = 1; k <= 8) — either works, just be consistent to avoid off-by-one errors.

A few extra notes that clarify behavior and common pitfalls:

  • The enhanced for used inside the original print will iterate from the stack’s bottom (oldest) to top (newest) because Stack inherits Vector iteration order. To display top-first without destroying the stack, iterate by index from size()-1 down to 0 and use get(i).
  • Don’t use pop() for printing unless the intent is to remove items — pop() is destructive. Use get, peek(), or an index-based loop to inspect contents safely.
  • Modern advice: Stack is a legacy synchronized class. For new code prefer Deque<String> stack = new ArrayDeque<>(); which offers push/pop/peek with better performance.

Troubleshooting: ensure imports (java.util.Stack or java.util.ArrayDeque) are present, the print method signature matches Stack<String>, and the method is accessible from main (static or via an instance).

Recommended Answers

All 5 Replies

create an instance of the class Stack<String>, let's suppose it's called "theStack", then just pass that to the print method:
print(theStack);

Hi thanks. Can u please also tell me how I can Use a for loop to help the cook assemble a Stack of eight wheat pancakes. Complete the for loop and add the missing statement so the eight pancakes are placed on the stack.

public class PancakeTest {

      public static void main(String[] args) {
       Stack<String> pancakeStack= new Stack<String>();
       Scanner keyboard = new Scanner(System.in);
       //Bonus pancakes are either bluebery or strawberry. 
String buckwheat = "Buckwheat Pancake"; // Standard pancake 
String blueberry = "Blueberry Pancake"; // Bonus pancake 
String strawberry = "Strawberry Pancake"; // Bonus pancake 
//Use a for loop to help the cook assemble a Stack of eight buckwheat pancakes. Complete the for loop and add the missing statement so the eight pancakes are placed on the stack. 
for(int k = 1; k<=8 ; k++){

That's a very specific homework question, and the answer is one very short line of code. You should make a greater effort to solve it yourself, rather than ask us to help you cheat. Post what you have tried, and someone will help fromn there.

Yeah the little code that I came up with is ,

for(int k = 1; k<=8 ; k++){

// But im not sure if it's correct. Because I dont know how to use this for loops to cook a stack of eight Buckwheat pancakes

All you need the loop for is to count up to eight. Inside the loop you just need to create a new pancake and add it to the stack. The loop runs 8 times, so you create and add 8 panckaes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.