How do I implement the toString() method for an ArrayStack?

10,056

You can override toString() method in ArrayStack as shown here. This will solve your problem.

public String toString() {
    String result = "";

    for (int scan = 0; scan < top; scan++)
        result = result + stack[scan].toString() + "\n";

    return result;
}
Share:
10,056
helloworld
Author by

helloworld

Updated on June 05, 2022

Comments

  • helloworld
    helloworld almost 2 years

    I want to display a list of orders of type ArrayQueue <Order> The class Order has an ArrayStack<String> as one of its attributes. I overrode the toString() method in the class Order, but how do I override it in the ArrayStack class? Because this is the output I get when I display:

    OrderNumber Name Date ArrayStack@481adc30

    What would I have to do to display the Strings in ArrayStack correctly? Do I make changes to class ArrayStack or change something in my Display method?

    This is my Display method:

     public void display(){
        if (!isEmpty())
        for (int i = 0; i < numberOfEntries; i++) {
            System.out.println(queue[(frontIndex + i) % queue.length]);
        }
        else System.out.println("You don't have any orders");
        }
    

    ArrayStack Class:

     public class ArrayStack < T > implements StackInterface < T >
    {
        private T [] stack; // array of stack entries
    
        private int topIndex; // index of top entry
    
        private static final int DEFAULT_INITIAL_CAPACITY = 50;
    
        public ArrayStack ()
        {
            this (DEFAULT_INITIAL_CAPACITY);
        } // end default constructor
    
    
        public ArrayStack (int initialCapacity)
        {
            // the cast is safe because the new array contains null entries
            @ SuppressWarnings ("unchecked")
                T [] tempStack = (T []) new Object [initialCapacity];
            stack = tempStack;
            topIndex = -1;
        } // end constructor
    
        /*  Implementations of the stack operations */
    

    Order Class:

       import java.util.Date;
    
    
    public class Order {
    
        int orderNumber;
        String customerName;
        Date date;
        StackInterface <String> items;
    
    Order( int number, String name, Date datum, StackInterface<String> item){
        orderNumber = number;
        customerName= name;
        date= datum;
        items = item;   
    }
    
    /Overriding toString() to Display a list of Orders as one String line. 
    public String toString(){
        return orderNumber + " " + customerName + " " + date + " " + items;
    }
    
  • helloworld
    helloworld about 9 years
    where would I add this?
  • Master Mind
    Master Mind about 9 years
    you would add it to your in place of for (int i = 0; i < numberOfEntries; i++) { System.out.println(queue[(frontIndex + i) % queue.length]); }
  • helloworld
    helloworld about 9 years
    but that would print nulls if I have only one object in my queue
  • mel3kings
    mel3kings about 9 years
    yep otherwise do the printout per Order.
  • helloworld
    helloworld about 9 years
    That would still print the ArrayStack attribute inside the ArrayQueue as ArrayStack@481adc30 and it will also display nulls if I have only one object in my Queue
  • helloworld
    helloworld about 9 years
    It didn't fix the ArrayStack@481adc30 problem when I call Display
  • mel3kings
    mel3kings about 9 years
    please put ArrayStack and Order codes in the question as well
  • helloworld
    helloworld about 9 years
    Yaay! It kinda worked! but it displayed only the first item in the stack. Why? could it be a mistake in the way I added items to the stack? int no = input.nextInt(); StackInterface <String> items = new ArrayStack<String>(no); for(int j= 1; j<= no; j++){ System.out.println("Enter item no: " + j ); String theItem = input.next(); items.push(theItem); } Order order1 = new Order (orderno, customername, real_date, items); orders.enqueue(order1);
  • Naman Gala
    Naman Gala about 9 years
    Please put it into your question, it is difficult to read code in comments.
  • Naman Gala
    Naman Gala about 9 years
    Show the push method as well as enqueue method.
  • helloworld
    helloworld about 9 years
    Nevermind. It worked now! It wasn't printing the last item but I changed scan < top to scan<=top and it works perfect now. Thank you so much! :)
  • Naman Gala
    Naman Gala about 9 years
    Your welcome. Please accept the answer if it helped you.