Implementing a queue using three stacks This is just for fun so you will see tons of print statements for trouble shooting. I was hoping on slimming this down some and gain new ideas on what I have. This was a challenge question in a class I'm not in. I know the code i wrote is nightmare-ish anything to slim it down would be nice. 

import java.util.*;


public class StackQueue {

    Stack stack1 = new Stack();
    Stack stack2 = new Stack();
    Stack temp   = new Stack();


    public void enqueue(String element) {
        stack1.push(element);
        System.out.println(stack1.toString() + "Stack1");
    }


        public void dequeue(){
            System.out.println(stack1.toString() + "\n\nStack1");

            if(!stack1.empty() &amp; stack1.size()&gt; 1 &amp; !stack2.empty()) {           // if there are elements in both stacks
                System.out.println("IM IN THE BLOCK");
                while (!stack2.empty()) {
                    temp.push(stack2.pop());
                    System.out.println(temp.toString() + " TEMPP----");
                    System.out.println(stack2.toString() + " STACK 2---");
                }
                while (!temp.empty()) {
                    stack1.push(temp.pop());
                    System.out.println(stack1.toString() + " STACK1 ----");
                }
                System.out.println(stack1.pop() + " Has been removed");
                System.out.println(stack1.toString() + "STACK 1----");
                System.out.println(stack2.toString()+ "STACK 2----");

                if(stack2.empty()){
                    while (!stack1.empty()) {
                        temp.push(stack1.pop());
                        System.out.println(temp.toString() + " temp ----");
                    }
                    System.out.println(stack1.addAll(temp));
                    System.out.println(stack1.toString()+ "STACK ! ADD");
                    temp.removeAllElements();
                    System.out.println(temp.toString() + "TEMP _++++_+_+_+_");
                }
                }



           else if(stack1.empty()){

                System.out.println("\n\nstack1 is empty ");
                System.out.println(stack2.pop() + " Has been removed");
            }else {
                System.out.println("\n\n");
                while (!stack1.empty()) {
                    System.out.println(stack2.toString() + "Stack2 - second part");
                    stack2.push(stack1.pop());

                }
            System.out.println(stack1.toString()+ "stack1");
            System.out.println(stack2.toString()+"stack2");
            System.out.println(stack2.pop() + " Has been removed");
                    }

        }
    }






public class Main {
    public static void main(String[] args){
        StackQueue queue = new StackQueue();

        queue.enqueue("hey");
        queue.enqueue("I");
        queue.enqueue("am");
        queue.enqueue("Jeremiah");
        queue.enqueue("now");
        queue.dequeue(); //removes hey
        queue.dequeue(); //removes i
        queue.dequeue();// removes am
        queue.enqueue("one ");
        queue.enqueue("more");
        queue.enqueue("time");
        queue.dequeue();  // removes jeremiah

        // i had to reverse the stack1 order here 
        queue.dequeue(); // removes now
        //queue.dequeue(); // removes one
        queue.enqueue("FIRST");
        queue.dequeue();//removes FIRST


        System.out.println(queue.stack1.toString()  + " END S1");
        System.out.println( queue.stack2.toString() + " END S2");
        System.out.println(queue.temp.toString() + " END temp");


    }

}
import java.util.Stack;

public class StackQueue&lt;T&gt; {
    private Stack&lt;T&gt; writeStack = new Stack&lt;T&gt;();
    private Stack&lt;T&gt; readStack = new Stack&lt;T&gt;();

    public void enqueue(T element) {
        writeStack.push(element);
    }

    public T dequeue(){
        if (readStack.empty()) {
            while (!writeStack.empty()) {
                readStack.push(writeStack.pop());
            }
        }
        return readStack.pop();
    }
}


Algorithm

You only need two stacks. Always write to the writeStack, read from the readStack. If the latter is empty, copy everything over.

Naming

Use meaningful names, such as writeStack and readStack instead of stack1 and stack2. Declare them private. A queue's user doesn't care about the internals. Import only what you need, in this case java.util.Stack.

Generics

The Stack&lt;T&gt; is a generic datatype, make use of it. Make your StackQueue generic as well. Instanciate it using

StackQueue&lt;String&gt; queue = new StackQueue&lt;String&gt;();


Return value

dequeue() should return a value. Print it with

System.out.println(queue.dequeue());