Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 580353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:27:49+00:00 2026-05-13T14:27:49+00:00

I am inputting this text file into the command line: 800 5 10 800

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:27:50+00:00Added an answer on May 13, 2026 at 2:27 pm

    +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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.