Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Priority queue to sort integer inputs
    primarykey
    data
    text
    <p>I'm trying to sort a sequence of positive integers which are inputted by the user with the use of Priority Queue. I'm not sure how to implement something so I'm wondering if anyone could help.This is what I have so far:</p> <pre><code>import java.util.PriorityQueue; import java.util.Scanner; public class priorityquene implements Comparable&lt;priorityquene&gt; { final int priority; final String name; public priorityquene(int p, String n) { priority = p; name = n; } public String toString() { return priority + ", " + name; } public int compareTo(priorityquene other) { return priority &lt; other.priority ? -1 : priority &gt; other.priority ? 1 : 0; } public static void main(String[] args) { PriorityQueue&lt;priorityquene&gt; pq = new PriorityQueue&lt;priorityquene&gt;(); Scanner input = new Scanner(System.in); System.out.println("Please enter a positive integer: "); int num1 = input.nextInt(); pq.add(new priorityquene(num1, " ")); System.out.println("Please enter a positive integer: "); int num2 = input.nextInt(); pq.add(new priorityquene(num2, " ")); System.out.println("Please enter a positive integer: "); int num3 = input.nextInt(); pq.add(new priorityquene(num3, " ")); System.out.println("Please enter a positive integer: "); int num4 = input.nextInt(); pq.add(new priorityquene(num4, " ")); System.out.println("Please enter a positive integer: "); int num5 = input.nextInt(); pq.add(new priorityquene(num5, " ")); while (!pq.isEmpty()) System.out.println(pq.remove()); } } </code></pre> <p>There are some things I'm not sure how to implement. How do i set it to accept an unlimited inputs and when a specific integer is input, say 0; it begins to sort from smallest to highest the inputs so far prior to 0?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload