Unable to create an instance of type Queue. Why is this?

This is my main method for assigning stacks / queues. I keep getting an error with my queue, but not my stack. The stack class seems to work fine. I am completely stuck. It states that "it is not possible to create an instance of type Queue." Any help would be greatly appreciated!

public class mainMeth { public static void main(String[] args) throws FileNotFoundException { File Polish = new File("fILE4INPUT.txt"); File out = new File("outfile.txt"); Scanner f = new Scanner(Polish); Queue inputQ = new Queue(); Stack stack2 = new Stack(); Queue outputQ = new Queue(); String word; Character ch; while (f.hasNext()) { String myString = f.nextLine(); for (int count = 0; count < myString.length(); count++) { ch = myString.charAt(count); inputQ.addtoRear(ch); } while (!inputQ.ismtQ()) { ch = inputQ.remfront(); if (isAlpha(ch)) { // System.out.println(ch); outputQ.addtoRear(ch); } else { if (isOperator(ch)) { if (stack2.ismt()) { stack2.push(ch); } else { if (valueOf(ch) > valueOf(stack2.top())) { stack2.push(ch); } else { outputQ.addtoRear(stack2.pop()); stack2.push(ch); } } } } } while (!stack2.ismt()) { outputQ.addtoRear(stack2.pop()); } System.out.println(outputQ.toString() + "\n\n"); while (!outputQ.ismtQ()) { outputQ.remfront(); } } } public static boolean isAlpha(Character ch) { boolean retVal = false; if (ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z') retVal = true; return (retVal); } public static boolean isOperator(Character ch) { boolean retVal = false; if (ch == '+' || ch == '-' || ch == '/' || ch == '*') retVal = true; return (retVal); } public static int valueOf(Character ch) { int retval = 0; if (ch == '/' || ch == '*') retval = 2; else retval = 1; return retval; } } 
+7
java queue
source share
4 answers

In the Interfaces section of Java docs:

Interfaces cannot be created: they can only be implemented by classes or extended by other interfaces.

Then you cannot directly instantiate the interface Queue<E> . But you can still refer to an object that implements the Queue interface by type of interface, for example:

 // As I saw that you are adding Characters to your queue Queue<Character> inputQ = new PriorityQueue<Character>(); 

You can choose the appropriate implementation to use according to your requirements, here is a list of all concrete and known implementing classes from it java docs :

ArrayBlockingQueue , ArrayDeque , ConcurrentLinkedDeque , ConcurrentLinkedQueue , DelayQueue , LinkedBlockingDeque , LinkedBlockingQueue , LinkedList , LinkedTransferQueue , PriorityBlockingQueue , PriorityQueue , SynchronousQueue

+9
source share

In Java, Queue is an interface; you cannot directly create Queue . See the documentation here . Please use something like this:

 Queue<String> queue = new LinkedList<String>(); 
+3
source share

because the queue is the interface. check the oracle specifications to evaluate the specific class that can be created. link

+2
source share

Java Queue is an interface and cannot be installed. You need a specific class that implements Queue.

+2
source share

All Articles