Hello, for this assignment, we have to imitate SW Airline's boarding process. I think I have everything working so far, except for the fact that my program ignores every Passenger entered into the queue except the first one. It's almost as if it bypasses the if statement asking if the queue is empty. Any help would be greatly appreciated.

import java.util.ArrayList;

public class LinkedQueue implements PriorityQueue<Passenger>{
	private ArrayList<LinkedNode> Q = new ArrayList<LinkedNode>();
	/**
	 * Is there anything in the queue?
	 * @return true if the queue is empty; otherwise return false
	 */
	public boolean isEmpty(){
		if(Q.size() == 0){
			return true;
		}
		else if(Q.size() > 0){
			return false;
		}
		else{
			return false;
		}
	}

	/**
	 * Add passenger to the queue (at the appropriate location).
	 * @param toInsert Passenger to be added
	 */
	public void enqueue(Passenger toInsert){
		if(Q.isEmpty() == true){
			LinkedNode<Passenger> thisToInsert = new LinkedNode<Passenger>(toInsert);
			Q.add(thisToInsert);
		}
		if(Q.isEmpty() == false){
			LinkedNode<Passenger> thisToInsert = new LinkedNode<Passenger>(toInsert);
			for(int i = 0; i == Q.size(); i++){
				int compared = thisToInsert.getValue().compareTo(Q.get(i).getValue());
				if(compared < 0){
					thisToInsert.changeNext(Q.get(i));
					Q.add(i, thisToInsert);
					Q.get(i - 1).changeNext(thisToInsert);
				}
				if(i == (Q.size() - 1)){
					Q.add(Q.size(), thisToInsert);
				}
			}
		}
	}

	/**
	 * Removes and returns the item at the front of the queue.
	 * @return the removed element
	 */
	public Passenger dequeue() {
		LinkedNode nextNode = Q.get(0);
		Passenger nextPass = nextNode.getValue();
		Q.remove(0);
		return nextPass;
	}
}

That's the offending program.

How should I fix this?

The offending area is around line 30.

I've narrowed the problem down, with print statements to the for loop in general, which is being ignored and bypassed completely. Why would this be?

So... I updated the code, and now I see that the for loop gets called but the array never becomes larger than 1. I figured this out with print statements. I have literally no idea why this is happening...

import java.util.ArrayList;

public class LinkedQueue implements PriorityQueue<Passenger>{
	private ArrayList<LinkedNode> Q = new ArrayList<LinkedNode>();
	/**
	 * Is there anything in the queue?
	 * @return true if the queue is empty; otherwise return false
	 */
	public boolean isEmpty(){
		if(Q.size() == 0){
			return true;
		}
		else if(Q.size() > 0){
			return false;
		}
		else{
			return false;
		}
	}

	/**
	 * Add passenger to the queue (at the appropriate location).
	 * @param toInsert Passenger to be added
	 */
	public void enqueue(Passenger toInsert){
		System.out.println(Q.isEmpty());
		System.out.println(Q.size());
		if(Q.isEmpty()){
			LinkedNode<Passenger> thisToInsert = new LinkedNode<Passenger>(toInsert);
			Q.add(thisToInsert);
		}
		else if(! Q.isEmpty()){
			System.out.println("HEYYYYYYYYYYYYYYYYYYYYYYYYY");
			LinkedNode<Passenger> thisToInsert = new LinkedNode<Passenger>(toInsert);
			System.out.println("OHHHHHHHHHHHHHHHHHHHHHHHHHHH");
			for(int i = 0; i < Q.size(); i++){
				System.out.println("BLARRRRRRRCH");
				int compared = toInsert.compareTo(Q.get(i).getValue());
				if(compared == -1){
					System.out.println("YOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
					thisToInsert.changeNext(Q.get(i));
					Q.add(i, thisToInsert);
					Q.get(i - 1).changeNext(thisToInsert);
				}
				if(i == (Q.size())){
					Q.add(Q.size(), thisToInsert);
				}
			}
		}
		System.out.println(Q.isEmpty());
		System.out.println(Q.size());
	}

	/**
	 * Removes and returns the item at the front of the queue.
	 * @return the removed element
	 */
	public Passenger dequeue() {
		System.out.println(Q.isEmpty());
		System.out.println(Q.size());
		LinkedNode nextNode = Q.get(0);
		Passenger nextPass = nextNode.getValue();
		Q.remove(0);
		System.out.println(Q.isEmpty());
		System.out.println(Q.size());
		return nextPass;
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.