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 + "]";
}
}