views:

190

answers:

3

I am inputting this text file into the command line:

800    5    10  
800    1    8  
800    7    7  
810    2    9  
845    2    10  
850    1    3  

first column is time,then priority,then length
the output is sorted by time plus the length, and the next item is outputted if the time of the item is less than or equal to the current time, with the highest priority going first (yes its confusing, but its a class project)

which will output:

Job: [800,1,8] Start time: 800 End time:808  
Job: [800,5,10] Start time: 800 End time:818  
Job: [810,2,9] Start time: 810 End time: 819  
Job: [800,7,7] Start time: 800 End time: 807   
Job: [845,2,10] Start time: 845 End time 855   
Job: [850,1,3] Start time: 850 End time: 853     

im using a linked list with queue methods called event, and a priority queue called queue
my problem is that the while loop while (event.size() != 0 && queue.size() != 0) is not executing at all.
If i change it to a do while loop, I get null pointer exception errors on while (event.peek().time <= currentTime) and if (event.peek().time > currentTime) I have tried fixing the null pointer exceptions by adding event.peek() != null and it still doesnt work. Event (linkedlist) has 6 Job objects in it so I dont know why event.peek() is returning null.

import java.util.*;
import java.io.*;

public class pj2
{   
    Queue<Job> event = new LinkedList<Job>();//interface queue
    PriorityQueue<Job> queue = new PriorityQueue<Job>();

    public static void main(String[] args) throws IOException
    {
        if (args.length != 1)
        {   
            System.out.println("Usage: java pj2 jobs.txt");
            System.exit(0);
        }
        else
            new pj2(args[0]);
    }

    public pj2 (String textFile) throws IOException
    {       
        File file = new File(textFile);
        if (!file.exists())
        {
            System.out.println(textFile + " does not exist.");
            System.exit(0);
        }

        //add time,priority,length to event queue
        Scanner data = new Scanner(file);
        while (data.hasNext())
        {
            int time = Integer.parseInt(data.next());
            int priority = Integer.parseInt(data.next());
            int length = Integer.parseInt(data.next());
            Job temp = new Job(time,priority,length);
            event.add(temp);
        }
        data.close();

        int currentTime = 0;
        //loop through priority queue, outputting results
        while (event.size() != 0 && queue.size() != 0)
        {
            if (queue.size() == 0)
                currentTime = event.peek().time;
            while (event.peek().time <= currentTime)
            {
                currentTime += event.peek().length;
                queue.offer(event.poll());
            }

            if (event.peek().time > currentTime)
            {
                currentTime = (event.peek().time + event.peek().length);
                queue.offer(event.poll());
            }

                System.out.println(queue.peek() + " Start time: " + queue.peek().time + " End time: " + (queue.peek().time + queue.peek().length));
                queue.poll();
        }

    }
}

public class Job implements Comparable<Job>
{
    int time, length, priority;

    public Job(int time, int priority, int length)
    {
        this.time = time;
        this.priority = priority;
        this.length = length;
    }
    public int compareTo(Job that)
    {
        if (this.priority == that.priority)
            return this.time - that.time;
        return this.priority - that.priority;
    }
    public String toString()
    {
        return "[" + time + "," + priority + "," + length + "]";
    }

}
+1  A: 

my problem is that the while loop while (event.size() != 0 && queue.size() != 0 ) is not running

What does that mean? Are values not what you expect? Did you debug it? If not, I encourage you to properly use your debugger.

JRL
the while loop is not executing at all
Raptrex
His point is that the tools are at your disposal to figure out why it is not executing. If you don't learn how to use these tools, you'll have severe difficulty completing the course.
Arkaaito
A: 

+1 to previous poster's pointer to the debugger tutorial. If you look at the values being found when the loop condition is evaluated, you will be able to find this problem instantly. ("Oh, event.size() != 0 && queue.size() != 0 is false because queue.size() != 0 is false.")

Generally, your thought process for solving problems like this should be, "If the while loop is not executing, its condition is false. I need to find out which part of the condition is false. From there I can figure out whether the bug is 'the previous part of my program is making this false where it should be true', or 'this shouldn't actually be part of the loop condition'."

In this case, you require event.size() != 0 AND queue.size() != 0, but you don't add anything to queue prior to the loop. I think you need to rethink your loop conditions.

Arkaaito
A: 

While reading the input file you fill the event list, but filling the queue is left to the while loop, so your condition:

    while (event.size() != 0 && queue.size() != 0)

never will be true as the queue is still empty. So either you need to change your loop condition to:

    while (event.size() != 0 || queue.size() != 0)

to handle information as long as either the list or the queue still holds data, or you might have to split your loop into 2 loops, one to fill the queue from the list followed by one that uses the queue to determine your answer.

rsp