1
votes

What I am trying to to output:

Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune

A ninth planet has been found! What is it's name?: Pluto

Which planet do you wish to blow up?: Mercury

Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto

Question: Can anyone lead me in the right direction as to how to fix my commented out method addNode? And yes, I know it is completely wrong.

Class NodePlanets.java

import java.util.Scanner;

public class NodePlanets
{
   public static void main(String[] args)
   {
      Node p, q, r;

      p = new Node("Mercury");
      q = p;

      r = new Node("Venus");
      q.next = r;
      q = r;

      r = new Node("Earth");
      q.next = r;
      q = r;

      r = new Node("Mars");
      q.next = r;
      q = r;

      r = new Node("Jupiter");
      q.next = r;
      q = r;

      r = new Node("Saturn");
      q.next = r;
      q = r;

      r = new Node("Uranus");
      q.next = r;
      q = r;

      r = new Node("Neptune");
      q.next = r;
      q = r;

      System.out.println(printLinkedListNodes(p));
      //addNode(p);
      System.out.println(printLinkedListNodes(p));
      removeNode(p);
      System.out.println(printLinkedListNodes(p));
   }//end main

   public static String printLinkedListNodes(Node nodePassed)
   {
      if(nodePassed.next == null)
         return nodePassed.data;
      else
         return nodePassed.data + " " + printLinkedListNodes(nodePassed.next);
   }//end method

/*

   public static void addNode(Node list, String userInput)
   {
      Scanner ui = new Scanner(System.in);
      System.out.println("A ninth planet has been found! What is it's name?");
      userInput = ui.nextLine();
      Node prev = null;
      Node curr = list;
      while (curr != null)
      {  
         prev = curr;
         curr = curr.getNext();
      }
      prev.setNext(new Node(userInput,null)); 
   }//end method   

*/

   public static Node removeNode(Node nodePassed)
   {
      Scanner ui = new Scanner(System.in);
      String userInput;
      System.out.println("Which Planet do you wish to blow up?");
      userInput = ui.nextLine();
      Node currentNode = nodePassed;
      Node temp;
      if(userInput.equalsIgnoreCase(currentNode.data))
      return currentNode.next; 
      while(currentNode.next!=null)
      {
         temp = currentNode.next.next;
         if(userInput.equalsIgnoreCase(currentNode.next.data))
         {
            currentNode.next = temp;
            return nodePassed;
         }
         currentNode = currentNode.next;
      }//endwhile
      return nodePassed;
   }//end method

}//end class

class Node.java

public class Node
{
   public String data;
   public  Node next;

   public Node()
   {
      data = "";
      next = null;
   }//end method

   public Node(String s)
   {
      data = s;
      next = null;
   }//end methodC

   public String toString()
   {
      return "Data:" + data;
   }//end method

}//end class
1
First off, you should never ever take input inside of a method. That should be done in your main class. - sbowde4

1 Answers

0
votes

This method should definitely not be a part of the calling client and rather should be a part of the Node class. Nonetheless this should get you going.

  public static void addNode(Node list)
       {
          Scanner ui = new Scanner(System.in);
          System.out.println("A ninth planet has been found! What is it's name?");
          String userInput = ui.nextLine();

          Node curr = list;
          while (cur.next != null)
          {  
             curr = curr.getNext();
          }
          curr.setNext(new Node(userInput, null));

       }//end method