this is my work class which adds works with their priority and ı used comparable interface to obtain priority among works.
public class Work implements Comparable<Work>{
private String work;
private int pri;
//this is constuctor
public Work(String work, int pri){
this.work = work;
this.pri = pri;
}
//this is implementation of compareTo method
@Override
public int compareTo(Work o) {
if (this.pri < o.pri)
return -1;
else
return 1;
}
@Override
public String toString() {
return this.work;
}
}
and this is the part which is in regard to the priority queue
int option = 0;
String work;
int urgency;
PriorityQueue<Work> works = new PriorityQueue<Work>();
do {
System.out.println("enter what you want\n1.add work\n2.next\n3.quit ");
option = scan.nextInt();
if (option == 1) {
System.out.println("what is work ");
work = scan.nextLine();
scan.nextLine();
System.out.println("how is your work urgency ");
urgency = scan.nextInt();
works.add(new Work(work, urgency));
}
if (option == 2) {
while (!works.isEmpty()) {
System.out.println(works.poll());
}
}
} while (option != 3);
while option 1 and 3 works well, option 2 is not working it understand the work just blank line and output is just a blank line. thanks in advance!!
sorry for giving not the necessary info because of StackOverflow rules(mostly code).