6

I am doing a queue in java. Here is my code:

public class ListQueue {
public static void main(String[] args){
    Queue myQueue;
    Scanner sc = new Scanner(System.in);
    String input;
    int choice = 99;
    do{
        System.out.println("================");
        System.out.println("Queue Operations Menu");
        System.out.println("================");
        System.out.println("1,Enquene");
        System.out.println("2,Dequeue");
        System.out.println("3,Empty?");
        System.out.println("4,Count?");
        System.out.println("5,View Queue");
        System.out.println("0, Quit\n");
        System.out.println("Enter Choice:");
        try{
            choice = sc.nextInt();
            switch(choice){
            case 1:
                System.out.println("Please enter name: ");
                input = sc.next();
                myQueue.enqueue(input);
                System.out.println(input + "is successful queued");
                break;
            case 2:
                if(myQueue.isEmpty()){

                }
                break;
            case 3:
                if(myQueue.isEmpty()){
                    System.out.println("Queue is empty");
                }else{
                    System.out.println("Queue is not empty");
                }
                break;
            case 4:
                System.out.println("Number of people is " + "the queue" + myQueue.size());
                break;
            case 5:
                if(!myQueue.isEmpty())
                    myQueue.viewQueue();
                else
                    System.out.println("Queue is empty");
                break;
            case 0:
                System.out.println("Good-bye");
                break;
            default:
                    System.out.println("Invalid choice");
            }
        }
        catch(InputMismatchException e){
            System.out.println("Please enter 1-5, 0 to quit");
            sc.nextLine();
        }
    }while(choice != 0);
}

}

However, I have error at the enqueue() and viewQueue() which I wonder why. Am I declare the queue in a wrong way? Thanks in advance. I am new to queue so please bear with me.

4 Answers4

13

Java queues don't have enqueue and dequeue methods, these operations are done using the following methods:

Enqueuing:

  • add(e): throws exception if it fails to insert the object
  • offer(e): returns false if it fails to insert the object

Dequeuing:

  • remove(): throws exception if the queue is empty
  • poll(): returns null if the queue is empty

Take a look to the first object in the queue:

  • element(): throws exception if the queue is empty
  • peek(): returns null if the queue is empty

The add method, which Queue inherits from Collection, inserts an element unless it would violate the queue's capacity restrictions, in which case it throws IllegalStateException. The offer method, which is intended solely for use on bounded queues, differs from add only in that it indicates failure to insert an element by returning false.

(see: http://docs.oracle.com/javase/tutorial/collections/interfaces/queue.html)

You can also check this as this is more useful:

http://docs.oracle.com/cd/B10500_01/appdev.920/a96587/apexampl.htm

Arpit Prajapati
  • 367
  • 2
  • 16
Manish Doshi
  • 1,205
  • 1
  • 9
  • 17
  • By the way how do I loop thru the element inside queue? Can you post me some example –  Jul 09 '13 at 12:46
3

You have not initialized myQueue:

Queue myQueue;

Note that Queue is an abtract class,you need initialize myQueue to the appropriate implementation.

Refer below javaDoc:

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Queue.html

sanbhat
  • 17,522
  • 6
  • 48
  • 64
ajay.patel
  • 1,957
  • 12
  • 15
1

@By the way how do I loop thru the element inside queue? Can you post me some example –

I have just added functionality in your Case 5: which is for viewing the List elements.

case 5:
    if(!myQueue.isEmpty())
    {
    for(Iterator it=myQueue.iterator();it.hasNext();)
        System.out.println(it.next());
//or using as a Object in Enhanced for loop
        //for (Object element : myQueue)
        //System.out.println(element);
    }
    else
        System.out.println("Queue is empty");
    break;

And as Ryman Holmes suggested you can use ArayDeque.

Queue<String> myQueue = new ArrayDeque<String>();

Benefits: - It is faster than Stack and LinkedList - ArrayDeque have no capacity restrictions so they can grow as necessary to support usage.

Risk: - They are not thread-safe; in the absence of external synchronization. - They do not support concurrent access by multiple threads.

coder9090
  • 63
  • 9
0

Like zerocool said you are not initializing the Queue, as Queue is an interface and you cannot instantiate an interface directly, you need to instantiate a class that implements the interface Queue such as ArrayDequeue, LinkedList etc...

To initialise the Queue and get rid of the errors you need something like this:

Queue<Integer> myQueue = new ArrayDeque<Integer>();

or

Queue<String> myQueue = new LinkedList<String>();

Here's the API for the type of Queues you can instantiate

Also see

Community
  • 1
  • 1
Ryman Holmes
  • 746
  • 3
  • 22
  • 40