title: Change all occurrences of an Int in a linked list using IntNode Java
body: <p>For my college work I need to make an IntList that includes the function</p>
  <pre><code>public void replace(int oldValue, int newValue);
  </code></pre>
  <p>The function itself needs to be able to replace all occurrences of an integer in the linked list. The list has is a singly linked list therefore has no previous function. Here is the rest of my code for understanding, any help is greatly appreciated.</p>
  <pre><code>import java.util.Scanner;
  
  public class IntListTest {
      private static Scanner scan;
      private static final IntList list = new IntList();
      //----------------------------------------------------------------
  // Creates a list, then repeatedly prints the menu and does what
  // the user asks until they quit.
  //----------------------------------------------------------------
      public static void main(String[] args)
      {
          scan = new Scanner(System.in);
          printMenu();
          int choice = scan.nextInt();
          while (choice != 0)
          {
              dispatch(choice);
              printMenu();
              choice = scan.nextInt();
          }
      }
      //----------------------------------------
  // Does what the menu item calls for.
  //----------------------------------------
      public static void dispatch(int choice)
      {
          int newVal;
          String info;
          switch (choice) {
              case 0 -&gt; System.out.println(&quot;Bye!&quot;);
              case 1 -&gt; { //add to front
                  System.out.println(&quot;Enter integer to add to front&quot;);
                  newVal = scan.nextInt();
                  list.addToFront(newVal);
              }
              case 2 -&gt; { //add to end
                  System.out.println(&quot;Enter integer to add to end&quot;);
                  newVal = scan.nextInt();
                  list.addToEnd(newVal);
              }         
              case 3 -&gt; {//print
                  list.print();
              }
              
              case 4 -&gt; {
                  int oldValue, newValue;
                  System.out.println(&quot;Enter the old value: &quot;);
                  oldValue = scan.nextInt();
                  System.out.println(&quot;Enter the new value: &quot;);
                  newValue = scan.nextInt();
                  list.replace(oldValue, newValue);
              }
              
              default -&gt; System.out.println(&quot;Sorry, invalid choice&quot;);
          }
      }
      //-----------------------------------------
      // Prints the user's choices
      //-----------------------------------------
      public static void printMenu()
      {
          System.out.println(&quot;\n Menu &quot;);
          System.out.println(&quot; ====&quot;);
          System.out.println(&quot;0: Quit&quot;);
          System.out.println(&quot;1: Add an integer to the front of the list&quot;);
          System.out.println(&quot;2: Add an integer to the end of the list&quot;);
          System.out.println(&quot;3: Print the list&quot;);
          System.out.println(&quot;4: Replace all occurrences of a value in the list with a new one.&quot;);
          System.out.print(&quot;\nEnter your choice: &quot;);
      }
  }
  
  public class IntList {
      private IntNode front;
  
      public IntList(){
          front = null;
      }
      public void addToFront(int val)
      {
          front = new IntNode(val,front);
      }
      //-----------------------------------------
  // Adds given integer to end of list.
  //-----------------------------------------
      public void addToEnd(int val)
      {
          IntNode new_node = new IntNode(val,null);
  
          if (front == null) {
              front = new_node;
          }
          else {
              IntNode temp = front;
              while (temp.next != null)
                  temp = temp.next;
  
              temp.next = new_node;
          }
      }
      
      //------------------------------------------------
      // Prints the list elements from first to last.
      //------------------------------------------------
      public void print()
      {
          System.out.println(&quot;--------------------&quot;);
          System.out.print(&quot;List elements: &quot;);
          IntNode temp = front;
          while (temp != null)
          {
              System.out.print(temp.val + &quot; &quot;);
              temp = temp.next;
          }
          System.out.println(&quot;\n-----------------------\n&quot;);
      }
      //-----------------------------------------
      // Replaces an exact element with a different one
      //-----------------------------------------
      public void replace(int oldValue, int newValue){
  
      }
  }
  public class IntNode {
      public int val; //value stored in node
      public IntNode next; //link to next node in list
      //------------------------------------------------------------------
  // Constructor; sets up the node given a value and IntNode reference
  //------------------------------------------------------------------
      public IntNode(int val, IntNode next)
      {
          this.val = val;
          this.next = next;
      }
  }
  </code></pre>