4 Replies - 1610 Views - Last Post: 08 December 2010 - 06:32 PM Rate Topic: -----

#1 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Priority Queue Scanning Help

Posted 08 December 2010 - 02:21 PM

I am working on a project and I need to find a way to scan a file into an object like:
Jobs[] job = new Jobs[100];
becuase i have a Jobs class that stores an arrival number, jobId, priority and length information
and then find a way to put that into a priority queue so that it can sorted by highest priority...
would it be easier to just scan the file into the priority queue and if so how do i implement the Jobs class so that it knows how to display and store the information correctly. Please help. My coding looks like this:
/*************************************************
 *
 ************************************************/
 import java.io.*;
 import java.util.Scanner;
 import java.util.*;

//this is my Job class that lists my jobId, length, priority and arrival
 class Jobs{
 		public String jobId;
 		public int length;
 		public int priority;
 		public int arrival;
 		
 		public Jobs(int arrival,String jobId, int priority, int length)
 		{	
 			this.jobId = jobId;
 			this.length = length;
 			this.priority = priority;
 			this.arrival = arrival;
 		}
 		public Jobs(int priority)
 		{
 			this.priority = priority;
 		}
 		
 		//set and get job Id character
 		public void setJobId(String jobId)
 		{
 			this.jobId = jobId;
 		}
 		public String getJobId()
 		{
 			return jobId;
 		}
 		
 		//set and get job length
 		public void setLength(int length)
 		{
 			this.length = length;
 		}
 		public int getLength()
 		{
 			return length;
 		}
 		
 		//set and get priority
 		public void setPriority(int priority)
 		{
 			this.priority = priority;
 		}
 		public int getPriority()
 		{
 			return priority;
 		}
 		
 		//set and get arrival
 		public void setArrival(int arrival)
 		{
 			this.arrival = arrival;
 		}
 		public int getArrival()
 		{
 			return arrival;
 		}
 		
 		
 		
 		//String to display job id, length, priority and arrival number
 		public String toString()
 		{
 			return jobId + "\t" + length + "\t" + priority + "\t" + arrival;
 		}
 	
 }
 
// this is my main class
class Main{
public static void main (String[] args) throws IOException{
 		int x;
 		int j = 0;
 		
 		
 		//reading in the information from the file
 		File file = new File("REVORD.DAT");
 		FileReader inputFile = new FileReader(file);
 		Scanner scan = new Scanner(file);
 		
 		//creating the array to hold the information
 		Jobs[] job = new Jobs[100];
 	   PriorityQueue queue =  new PriorityQueue(); 
 	   	
 
 	while(scan.hasNext())
 	{	
 	
  	}
 		System.out.println("Before Priority Order: ");

 	
 		//scanning in the file into the array by adding the elements to the queue
 	
 	  while(scan.hasNext())
		{	
			queue.addElement(job);
    		
		}
		
	//Removing the elements from the queue in the priority order
	System.out.println("Put in Priority Order: ");	
	while(!queue.isEmpty())
	{
		System.out.println(queue.removeNext()+ " ");
	}
	
	System.out.println();
 		}
 	}
 	




Is This A Good Question/Topic? 0
  • +

Replies To: Priority Queue Scanning Help

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10385
  • View blog
  • Posts: 38,434
  • Joined: 27-December 08

Re: Priority Queue Scanning Help

Posted 08 December 2010 - 03:30 PM

You should just read the Job objects directly into the PriorityQueue. No need for a Job[]. Also, is this your own custom PriorityQueue or the java.util.PriorityQueue?

As for implementing the Jobs class, make sure it implements the Comparable interface, and define the compareTo() method to order the Jobs based on priority.
Was This Post Helpful? 0
  • +
  • -

#3 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Re: Priority Queue Scanning Help

Posted 08 December 2010 - 03:43 PM

This is not a custom priorityqueue its the one i was given from java.util.priorityqueue.... I do not know how to read the information into my priority queue and have no clue where to start on implementing the comparable interface.. do i add "implements comparable" to my main class???
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10385
  • View blog
  • Posts: 38,434
  • Joined: 27-December 08

Re: Priority Queue Scanning Help

Posted 08 December 2010 - 03:50 PM

Since you are using the java.util.PriorityQueue, it should be generic. Since you want to store jobs, you should have a PriorityQueue<Job>. As for reading in the File, I can't advise on anything format specific since I haven't seen the format, but basically read in the necessary data for a single Job object, instantiate the Job, and add it to the PriorityQueue using the add() method.

As for the Job class, it should implement the Comparable interface there. So:
class Job implements Comparable<Job>{

    //the implemented method to satisfy the interface
    public int compareTo(Job other){
          //code to compare Jobs based on priority
    }
}



Note that the compareTo() method should return a negative number if this Job is less than the param, 0 if they are equal, and a positive number if this Job is greater than the param.
Was This Post Helpful? 0
  • +
  • -

#5 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Re: Priority Queue Scanning Help

Posted 08 December 2010 - 06:32 PM

so i changed my priority queue to look like this:
PriorityQueue<Jobs> queue =  new PriorityQueue<Jobs>(); 


i am not sure how to read information into it though.. thats where i am struggling the most... i tried using a while(scan.hasNext()) method but not sure what to put inside the {} to make it enter right... everything I enter keeps telling me incompatible type... my file looks like this; its a .dat file

1
abcdef
15
4
2
ghijkl
20
25


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1