3 Replies - 228 Views - Last Post: 02 November 2017 - 12:58 PM Rate Topic: -----

#1 aemaeth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 10-February 17

Java IntNode "cannot find symbol error"

Posted 02 November 2017 - 11:55 AM

Hey guys , I'm copying code out the book to work on IntLinkedBags this is the code
public class IntLinkedBag
{
  private IntNode head;
   private int manyNodes;
   
   public IntLinkedBag()
  {
    head = null;
    manyNodes = 0;
  }
  
  public void add(int element)
  {
    head = new IntNode(element,head);
    manyNodes++;
  }
}



I got the "cannot find symbol error" and I know it has to do with the class being named IntLinkedBag and probably using the "IntNode" type for the private instance variable "head". This code was taken straight verbatim from the book I'm using.
Posted Image

Is This A Good Question/Topic? 0
  • +

Replies To: Java IntNode "cannot find symbol error"

#2 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 590
  • View blog
  • Posts: 2,487
  • Joined: 13-June 14

Re: Java IntNode "cannot find symbol error"

Posted 02 November 2017 - 12:00 PM

Where is IntNode defined? Have you imported that class?
Was This Post Helpful? 1
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3447
  • View blog
  • Posts: 15,614
  • Joined: 20-September 08

Re: Java IntNode "cannot find symbol error"

Posted 02 November 2017 - 12:56 PM

You are using the default package ('no package') There must either be source code in the same directory for class IntNode (for some compilers) or the compiled class of that name in the classpath

Be careful with your indentation See
http://technojeeves....-code-formatter
Was This Post Helpful? 1
  • +
  • -

#4 aemaeth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 10-February 17

Re: Java IntNode "cannot find symbol error"

Posted 02 November 2017 - 12:58 PM

Thanks guys. The book only listed the above code, so I figured that was sufficient. I thought about it logically and added the static nested class for the definition of the "IntNode" and everything is working now. Thank you.

public class IntLinkedBag
{
   private IntNode head;
   private int manyNodes;
   
   private static class IntNode
   {
     private int data;
     private IntNode link;
     
     public IntNode(int initialData, IntNode initialLink)
     {
       data = initialData;
       link = initialLink;
     }
     
   }
  
  public void add(int element)
  {
    head = new IntNode(element,head);
    manyNodes++;
  }
  


  public int getLength()
  {
    return manyNodes;
  }
  
  public void traverse()
  {
    IntNode cursor;
    for(cursor=head; cursor!=null; cursor=cursor.link)
    {
      System.out.println(cursor.data);
    }
  }

   /* remove method using list search, need to write getLink method and getData method though because private variables
  so I reckon fuck this method */
  
  /*
    public void remove(int target)
    {
      IntNode targetNode;
      targetNode = IntNode.listSearch(head, target);
      if(target == null)
        return false;
      else
        targetNode.setData(head.getData());
      head = head.getLink();
      manyNodes--;
      return true; 
    }
    */
  
  public void remove(int target)
  {
    IntNode prev = head;
    IntNode curr;
    for(curr=head.link; curr!=null; curr=curr.link)
    {
      if(curr.data == target)
        prev.link = prev.link.link;
      prev = prev.link;
    }
  }
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1