i am doing a project with a group i have to deal with orders by kind then linked to the driver who will be taking the first customer. the driver can be free or back in 10 min.
i have started with the order list with the customer, i have never used a priority queue so i am trying to have the queue sort the file.
this is an eg of the order file. it is a very big file so this is just piece of it
Name, Kind, PickUp, DropOff, TypeOfSerice, Requirements
Jacob Adani , urgent, Santa Barbara, Maracas Gardens, luxury, sunroof
Ethan Bhaskar, booked in advance, Usc, Port of Spain, minivan, 12 seater
Michael Amarnath ,urgent, Mountain View, Port of Spain, luxury
William Reagna ,booked in advance, Usc, Piarco, luxury
Alexander McCloud,urgent, Piarco, Riverside, luxury
Daniel Edmonson,booked in advance, Acono, St. Joseph, minivan, 16 seater

public class Orders1 {
    private String Name;
    private String Kind;
    private String PickUp;
    private String DropOff;
    private String TypeOfService;
    private String Requirements;



    public Orders1(String Name, String Kind, String PickUp, String DropOff, String TypeOfService, String Requirements) {
        this.Name = Name;
        this.Kind = Kind;
        this.PickUp = PickUp;
        this.DropOff = DropOff;
        this.TypeOfService = TypeOfService;
        this.Requirements = Requirements;
    }

    /**
     * @return the name
     */
    public String getName() {
        return Name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String Name) {
        this.Name = Name;
    }

    /**
     * @return the kind
     */
    public String getKind() {
        return Kind;
    }

    /**
     * @param kind the kind to set
     */
    public void setKind(String Kind) {
        this.Kind = Kind;
    }

    /**
     * @return the pick up
     */
    public String getPickUp() {
        return PickUp;
    }

    /**
     * @param pick up the pick up to set
     */
    public void setPickUp(String PickUp) {
        this.PickUp = PickUp;
    }

    /**
         * @return the drop off
         */
        public String getDropOff() {
            return DropOff;
        }

        /**
         * @param drop off the drop off to set
         */
        public void setDropOff(String DropOff) {
            this.DropOff = DropOff;
    }

    /**
         * @return the type of service
         */
        public String getTypeOfService() {
            return TypeOfService;
        }

        /**
         * @param type of service the type of service to set
         */
        public void setTypeOfService(String TypeOfService) {
            this.TypeOfService = TypeOfService;
    }

    /**
         * @return the requirements
         */
        public String getRequirements() {
            return Requirements;
        }

        /**
         * @param requirements the requirements to set
         */
        public void setRequirements(String Requirements) {
            this.Requirements = Requirements;
    }
}

the error are 10.

in line 12 cannot find symbol ; for class io exception also pointing at th I for IOException

in line 16 cannot find ; for class HeapQueue and pointing to H.

basically all the errors are like that so maybe if anyone can help me understand these arrors i would be able to fix the rest and see what else is wrong with my priority queue.

import java.util.*;
import java.util.Comparator;
import java.util.Scanner;
import ds.util.HeapPQueue;


public class Orders{

    //String companyOrders;


    public static void main(String []args) throws IOException{

        //Handle order request

        HeapPQueue<Orders1> orderList = new HeapPQueue<Orders1>();

        //order request are read from file company orders

        Scanner co = new Scanner(new FileReader("companyOrders.txt"));

        //Scanner cd = new Scanner(new FileReader("compnayDriver.txt"));

        //read file; insert each order into priority queue

        while ((companyOrders = Orders1.readCompanyOrders(co)) != null)
                orderList.push(companyOrders);

        //delete orders from priority queue and output information

        System.out.println("Name    Kind" +     "   PickUp      " +     "   DropOff     " +     "   TypeOfService   " +     "   Requirements");

        while (!orderList.isEmpty()){
            //remove order from priority queue and output it

            companyOrders = (Orders1)orderList.pop();
            System.out.printf("%s ", q);
        }

        System.out.println();

    }
}

yes can anyone tell me why i am getting the errors i have listed above and how to fix them?

oh and how exactly do you compare strings when one is just trying to sort the kind part of the file with urgent first and then booked in advance and still have everything else printed out.

12 cannot find symbol ; for class io exception also pointing at th I for IOException

The compiler can not find a definition for the symbol in the error message. You probalby need an import statement.

The other errror message looks like its been editted and not copied correctly. Could you copy the full text and not type it in.

i have one error now and it has to do with this piece of code

Comparator<String> comparator = new StringLengthComparator();

and the error says that non-static variable this cannot be referenced from static context. and it is pointing at the n in new.
i am using text pad and there is no option to copy the errors.

What is StringLengthComparator? Is it a class? Where is it defined?
Where is the statement you posted coded?
Is it in a static method?

Edited 4 Years Ago by NormR1

import java.util.*;
//import java.util.Comparator;
//import java.util.Scanner;
//import java.util.PriorityQueue;
//import java.io.IOExceoption;
import java .io.*;
//import java.io.FileReader;

public class Orders{

    //private String companyOrders;
    private String ordersList;


    public static void main(String []args) throws IOException{

        Comparator<String> comparator = new StringLengthComparator();

        //Handle order request

        PriorityQueue<String> queue = new PriorityQueue<String>(40, comparator);



        queue.add("booked in advance");
        queue.add("urgent");

        //order request are read from file company orders

        Scanner co = new Scanner(new FileReader("companyOrders.txt"));

        //Scanner cd = new Scanner(new FileReader("compnayDriver.txt"));

        //read file; insert each order into priority queue


        while (queue.size() != 0){
                System.out.println(queue.remove());
            }

        //delete orders from priority queue and output information

        //System.out.println("Name  Kind" +     "   PickUp      " +     "   DropOff     " +     "   TypeOfService   " +     "   Requirements");

        //while (!orderList.isEmpty()){
            //remove order from priority queue and output it

            //companyOrders = (Orders1)orderList.pop();
            //System.out.printf("%s ", q);
        }

        //System.out.println();


        public class StringLengthComparator implements Comparator<String>{



            public int compare(String x, String y){

                if (x.length() < y.length()){

                    return -1;
                }
                if (x.length() > y.length()){

                    return 1;
                }

                return 0;

    }
}
}

if i am completely wrong could someone suggest how to use a priority queue where you read th info below from a file and sort it by the kind where all urgent is above and the all booked in advance and have all other info go along with it.

Name, Kind, PickUp, DropOff, TypeOfSerice, Requirements
Jacob Adani , urgent, Santa Barbara, Maracas Gardens, luxury, sunroof
Ethan Bhaskar, booked in advance, Usc, Port of Spain, minivan, 12 seater
Michael Amarnath , urgent, Mountain View, Port of Spain, luxury
William Reagna ,booked in advance, Usc, Piarco, luxury
Alexander McCloud, urgent, Piarco, Riverside, luxury
Daniel Edmonson, booked in advance, Acono, St. Joseph minivan, 16 seater

non-static variable this cannot be referenced from static context

Either Move the code out of the static main() method or define the class as static.

when i move it out of static main or make the class static the error now becomes

PriorityQueue<String> queue = new PriorityQueue<String>(40, comparator);

cannot find symbol ;
variable class comparator

class Orders
PriorityQueue<String> queue = new PriorityQueue<String>(40, comparator);

     and it is pointing at the c in comparator

i moved it outside of static but inside the class Orders and then i even tried making class StringLengthComparator a static

ok i have restructured my code and i am successfully reading the file but my problem is that i don't know how to use the priority queue so i need help in how to use the PQ after reading the file and wanting it to sort by kind which has a couple of booked in advance and urgent where urgent comes first and then booked in advance this is an e.g of the file

and here is the code i have so far hopefully someone can help me to move from here.

The priority queue sorts your orders according to the comparator that you supply. The comparator must take two orders and determine which is highest priority - that's just some if tests using the urgent and booked in advance fields from the two orders. The StringLengthComparator in your code is just an example of how to write a comparator - its no use to you in this application, so you need to replace it with a comparator that compares two orders

ok if you look at the e.g of the file it has name, kind which has the urgent and booked in advance it has to and from etc right so when i use the if statements with urgent and booked in advance evrything else would go with it right? like whatever is in the same line with the first urgent and so forth?

All the data from one line is used to make one instance of Order, so all those values are bound together in that instance of Order. When you sort a queue or Orders it moves Orders up or down the queue, but each Order keeps all its own values together.

ok i tried a PQ but i am getting error where they are saying i need ; in the line with the last } here is the piece of code i did

PriorityQueue<Orders1> queue = new PriorityQueue(179, new Comparator<Orders1>()){

            public String compare(urgent booked_in_advance){

                if(booked_in_advance > urgent)

                return 1;

                else if(booked_in_advance < urgent)

                return-1;

                else

                return 0;

            }

        }

Check the documentation for the Comparator interface. You have the wrong number of parameters and the wrong return type in your compare method.

looking for an e.g of comparator interface and will try it have not have to compare anything so don't know how to use it

You don't have to look very far... you posted a perfectly good one yourself in this thread 3 days ago. Did you understand your own code? This is what you posted:

public class StringLengthComparator implements Comparator<String>{
    public int compare(String x, String y){
        if (x.length() < y.length()){
            return -1;
        }
        if (x.length() > y.length()){
            return 1;
        }
        return 0;
    }
}

ok reached panic mode can someone help me understand the comparator interface and the compare in a priority queue, cause no matter what i try i can't seem to get rid of that one nagging error.

ok this is everything can someone please tell me whats wrong and whats missing. been working on it for a while and it is just about due like tomorrow..

import java.io.*;
import java.util.*;
import java.util.PriorityQueue;
//import java.lang.Comparable;
import java.util.Comparator;

public class Orders1 implements Comparator {

    //public String compareTo(Orders1 other){
    //}

    private String Name;
    private String Kind;
    private String PickUp;
    private String DropOff;
    private String TypeOfService;
    private String Requirements;



    public Orders1(String Name, String Kind, String PickUp, String DropOff, String TypeOfService, String Requirements) {
        this.Name = Name;
        this.Kind = Kind;
        this.PickUp = PickUp;
        this.DropOff = DropOff;
        this.TypeOfService = TypeOfService;
        this.Requirements = Requirements;
    }

    /**
     * @return the name
     */
    public String getName() {
        return Name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String Name) {
        this.Name = Name;
    }

    /**
     * @return the kind
     */
    public String getKind() {
        return Kind;
    }

    /**
     * @param kind the kind to set
     */
    public void setKind(String Kind) {
        this.Kind = Kind;
    }

    /**
     * @return the pick up
     */
    public String getPickUp() {
        return PickUp;
    }

    /**
     * @param pick up the pick up to set
     */
    public void setPickUp(String PickUp) {
        this.PickUp = PickUp;
    }

    /**
         * @return the drop off
         */
        public String getDropOff() {
            return DropOff;
        }

        /**
         * @param drop off the drop off to set
         */
        public void setDropOff(String DropOff) {
            this.DropOff = DropOff;
    }

    /**
         * @return the type of service
         */
        public String getTypeOfService() {
            return TypeOfService;
        }

        /**
         * @param type of service the type of service to set
         */
        public void setTypeOfService(String TypeOfService) {
            this.TypeOfService = TypeOfService;
    }

    /**
         * @return the requirements
         */
        public String getRequirements() {
            return Requirements;
        }

        /**
         * @param requirements the requirements to set
         */
        public void setRequirements(String Requirements) {
            this.Requirements = Requirements;
    }

            /*public int compareTo(Object otherKind){

                            if(booked_in_advance() > urgent)

                            return 1;

                            else if(booked_in_advance < urgent)

                            return -1;

                            else

                            return 0;

            }*/

        public static void main (String []args) throws IOException
            {
            String out="";
                  BufferedReader in = new BufferedReader(new FileReader("companyOrders.txt"));
                String str;
                while ((str = in.readLine()) != null)
                 {
                    out+=str+"\n";
                }
                in.close();
                System.out.print(out);
    }
        //Orders1[] orders1 = new Orders1[100];
        PriorityQueue<Orders1> queue = new PriorityQueue<Orders1>(179, new Comparator<Orders1>()){

                public int compare(Orders1 urgent, Orders1 booked_in_advance){

                return urgent.getKind() - booked_in_advance.getKind();
            }


        }

}

PriorityQueue uses your comparator to know which orders have the highest priority. To do that it passes them to you two at a time and you have to say which is higher. So you method signature should be like:

public int compare(Orders1 orderA, Orders1 orderB)

now you can use those 2 params to access the kind, or type of service or whatever for both orders and decide which is highest priority, eg
if (orderA.getKind().equals("Urgent") && ! orderB.getKind().equals"Urgent)) // orderA has higher priority
You'll need a few if tests like those to cover all the possibilities, and you'll return +1 or -1 to say which has higher priority.

ok bare with me my statements look like this, those it look right because i am getting the same error.

PriorityQueue<Orders1> queue = new PriorityQueue<Orders1>(179, new Comparator<Orders1>()){

                public int compare(Orders1 orderA, Orders1 orderB){

                if(orderA.getKind().equals("urgent") && ! orderB.getKind().equals("urgent"))

                 if(orderA.getKind().equals("urgent") < orderB.getKind().equals("booked_in_advance"))

                 if(orderB.getKind().equals("booked_in_advance") > orderA.getKind().equals("urgent"))



                return 1;


            }


        }

That's the right general idea, but you're quite a long way from a valid syntax in your if statements. Time to review your course notes or check the web on how to code it tests.

when i changed my if statements the errors become <, > and Orders1 is not an abstract and does not overide abstract method compare(java.lang.Object, java.lang.Object) in java.util.Comaparator in public class Orders1 implements Comaparator{ and it ic pointing to c in class.

but when i put Abstract before class i am getting class, interface, or enum expected and it is pointing to A in abstract
whats wrong now

PriorityQueue<Orders1> queue = new PriorityQueue<Orders1>(2, new Comparator<Orders1>(){

                public int compare(Orders1 orderA, Orders1 orderB){

                if(orderA.getKind().equals("urgent") && ! orderB.getKind().equals("urgent")){
                    return 1;
                }
                 else if(orderA.getKind().equals("urgent") < orderB.getKind().equals("booked_in_advance")){
                    return 1;
            }
                 else if(orderB.getKind().equals("booked in advance") > orderA.getKind().equals("urgent")){

                return 1;

            }
            }


        });

Orders1 is not an abstract and does not overide abstract method compare(java.lang.Object, java.lang.Object) in java.util.Comaparator in public class Orders1 implements Comaparator{ and it ic pointing to c in class.

operator < cannot be applied to boolean, boolean

if(orderA.getKind().equals("urgent") < orderB.getKind().equals(booked_in_advance")){
This article has been dead for over six months. Start a new discussion instead.