title: What's wrong with sorted set code? My output should match sample
body: <p>Can anyone help on what's wrong with my code? Only the first input number shows up when I run it (it doesn't create a sorted list), the delete command isn't working, and the 'true' 'false' in exists command doesn't show up. My output should match the given sample I put at the end.</p>
  <p>The areas I had to fill in to make the code work are the areas after the TODOTODOTODO symbols which would be 44-61, 75-83, 97-105. I'm not sure where I went wrong in those areas and why it is not working correctly to give the desired output?</p>
  <pre><code> import java.util.Scanner;
  
  // Defines the a Sorted Set collection and implements a driver program in main
  public class SortedSet {
      // Define a basic element of a linked list
      private class LinkedNode {
          int x;  // Value stored in the node
          LinkedNode next;  // Reference to the next node in the list
      }
      
  LinkedNode front = null;  // Reference to the front of the singly linked list
  
  // Adds the integer x to the collection.
  // The resulting collection is sorted in increasing order and
  // does not contain any duplicate values.
  public void add(int x) {
      // Initialize a new node to be added to the collection
      LinkedNode newNode = new LinkedNode();
      LinkedNode cur = front;
      newNode.x = x;
      
      // Check if list is empty
      if (cur == null) {
          front = newNode;
      } 
      // If list is not empty, check if node should be placed in front
      else if (front != null) {
          if (newNode.x &lt; front.x) {
              newNode.next = front;
              front = newNode;
          }
          
          // If not in front, check for the middle or the end, or duplicate.
          else {
              // &lt;TODO&gt;&lt;TODO&gt;&lt;TODO&gt; 
              LinkedNode temp = cur;
              LinkedNode prev = cur;
              int middle = x;
              while (temp != null) {
                  if(temp.x &gt; newNode.x) {
                      middle = 1;
                      newNode.next = temp;
                      prev.next = newNode;
                  }
                  prev = temp;
                  temp = temp.next;
                  }
              if (middle == 0) {
                  prev = newNode;
                  }
              }
          }
      }
              
  // Deletes the integer x from the sorted set.
  // The remaining collection remains sorted and without duplicates.
  public void delete(int x){
      // Declare a new reference and initialize it to the front of the list
      LinkedNode cur = front;
      
      // Check if list is empty
      if (front == null) {
          System.out.print(&quot;There is nothing to delete!&quot;);
      } else {  // Not empty
          // Go through list, checking whether node is in the list, and delete if found
          // &lt;TODO&gt;&lt;TODO&gt;&lt;TODO&gt;
          LinkedNode prev = new LinkedNode();
          while (cur.x != x &amp;&amp; cur != null) {
              prev = cur;
              cur = cur.next;
              }
          if (cur != null)
              prev.next = cur.next;
          }
      }
  
  // Returns true if the integer x exists in the sorted set and false otherwise.
  public void exists(int x) {
      // Declare a new reference and initialize it to the front of the list
      LinkedNode cur = front;
      
      // Check if list is empty.
      if (front == null) {
          System.out.println(&quot;false&quot;);
      }
      
      // If not empty, check for the node.
      // &lt;TODO&gt;&lt;TODO&gt;&lt;TODO&gt;
      else {
          while (cur != null) {
              if (cur.x==x)
                  return;
              cur=cur.next;
              }
          return;
      }
  }
  
  // Returns a string representing the sorted set as a space separated list.
  public String toString() {
      String s = &quot;&quot;;
      
      LinkedNode cur = front;     
      while (cur!=null) {
          s+= cur.x + &quot; &quot;;
          cur = cur.next;
      }
      
      return s;
  }
  
  // Driver method
  public static void main(String[] args) {
      // Declare variables
      SortedSet sortedSet = new SortedSet();
      Scanner scan = new Scanner(System.in);
      String[] tokens;
      String command;
      int num;
      
      // Print header info
      System.out.println(&quot;Programming Fundamentals\n&quot;
              + &quot;NAME: Andres Reyes\n&quot;
              + &quot;PROGRAMMING ASSIGNMENT 4\n&quot;);
              
      // Enter command loop
      while (true) {
          // Prompt the user for a command
          System.out.print(&quot;Enter command: &quot;);
          String input = scan.nextLine();
          
          // Parse input
          if (input.equals(&quot;q&quot;)) break;  // user quits
          tokens = input.split(&quot;\\s&quot;);
          if (tokens.length &lt; 2) continue; // invalid input
          command = tokens[0];
          num = Integer.parseInt(tokens[1]);
          
          // Execute command
          if (command.equals(&quot;add&quot;)){
              sortedSet.add(num);
              System.out.println(sortedSet);
          } else if (command.equals(&quot;del&quot;)) {
              sortedSet.delete(num);
              System.out.println(sortedSet);
          } else if (command.equals(&quot;exists&quot;)) {
              sortedSet.exists(num);
          } else {
              System.out.print(&quot;Command does not exist&quot;);
          }
      }
      
      System.out.println(&quot;\nGood bye!&quot;);
  }
  </code></pre>
  <p>}</p>
  <p><a href="https://i.stack.imgur.com/2LZYM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2LZYM.png" alt="enter image description here" /></a></p>