简体   繁体   中英

Java: Inconvertible Types Error (Using A Queue of Arrays)

I'm working on a program that simulates a restaurant seating system. I'm having trouble enqueueing (putting an object in a queue) a Party (an object) without making the variables static in the Party Class because if I do that then when I dequeue, it over writes all the objects in the queue to be the same.

Here is my code for the main program:

public static void main (String [] args)
    throws IOException
{
    //get name of simulation file from user and "open" that file
    Scanner cin= new Scanner (System.in);

    System.out.println("--- Welcome to the La Food Restaurant Simulator! ---");
    System.out.println("Enter a data file name:");
    String filename= cin.next();

    Scanner fileread= new Scanner(new FileReader (filename));

    Queue Q= new QueueArray(100);

    boolean flag=true;

    while (flag==true)
    {
        char action= fileread.next().charAt(0);
        int seatedtime;
        System.out.println(action); //TESTING PURPOSES ONLY

        //if A read in party and put them in at the back of the queue (enqueue)
        if(action=='A')
        {
            Party p= (Party)Q.enqueue(new Party(fileread));

            System.out.println(p); //TESTING PURPOSES ONLY
            //System.out.println(p.size); // TESTING PURPOSES ONLY
            //System.out.println(p.name); // TESTING PURPOSES ONLY

            //System.out.println("Please wait at the bar, party "+p.name+" of "+p.size+" people.");
        }

        //if T put the party at the front in the queue and remove them (dequeue)
        if(action=='T')
        {
            seatedtime=fileread.nextInt();
            System.out.println(seatedtime); //TESTING PURPOSES ONLY

            Party p2=(Party) Q.dequeue();
            System.out.println(p2.name);

            //need a way to return the front object's (that was dequeued) info (size,name,arrival)
            System.out.println("Table for "+p2.name "!");
        }

        // if Q stop the simulation
        if(action=='Q')
        {
            flag=false;
        }
    }

    System.out.println("---Simulation Terminated---");
    System.out.println("The average waiting time was ");
    System.out.println("The following parties were never seated:");
    //print out info on the unseated party
}
}

And here is my code for the Party Class:

public class Party
{

int arrival;
int size;
String name;

//constructor of party object
public Party(Scanner file)
{
    arrival=file.nextInt();
    size= file.nextInt();
    name= file.next();
    name= name + file.nextLine();
}
}

Here is the enqueue method used in the class of QueueArrays:

Object [] items; 
int front; 
int back;   
int count; 

public void enqueue(Object x) 
{  
    if(isFull()) return; // do nothing 
    count++; 
    back++; 
    f(back>=items.length) back=0; 
    items[back]=x; 
}

When I try to compile the main program it gives an error:

Inconvertible types on the line where I enqueue the party. Required: Party Found: void

How do I fix it without just using Q.enqueue() because that requires my variables in my Party Class to static which causes the overwrite problem?

Not sure if I get your question right, but the line Party p= (Party)Q.enqueue(new Party(fileread)); will not compile because your enqueue method returns void - you cannot assign that to a variable. Change it to

Party p = new Party();
Q.enqueue(p);

Still, I don't see any reason for anything to be static here.

BTW, why did you implement your own Queue and not use something like Queue<Party> q = new LinkedList<Party>(); (from the java.util package) - that would make your life a lot easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM