Results 1 to 2 of 2
  1. #1
    silvia is offline Member
    Join Date
    Jul 2007
    Posts
    35
    Rep Power
    0

    Default Error: cannot find symbol

    Please i need help.

    Java Code:
    class Node
    {
    
    public Object data;
    public Node next;
    
    public Node()
    {
    this.data = null;
    this.next = null;
    }
    
    public Node(Object a)
    {
    this.data = a;
    this.next = null;
    }
    
    public Node(Object a, Node b)
    {
    this.data = a;
    this.next = b;
    }
    
    }
    Java Code:
    public class List
    {
    private Node head;
    private Node tail;
    public List()
    {
    this.head = null;
    this.tail = null;
    }
    public void insertAtFront(Object obj)
    {
    if (this.size()==0)
    {
    Node n = new Node(obj);
    this.head = n;
    this.tail = n;
    }
    else
    {
    Node n = new Node(obj);
    n.next = this.head;
    this.head = n;
    }
    }
    public void insertAtBack(Object obj)
    {
    if (this.size()==0)
    {
    Node n = new Node(obj);
    this.head = n;
    this.tail = n;
    }
    else
    {
    Node n = new Node(obj);
    this.tail.next = n;
    this.tail = n;
    }
    }
    public Object removeFromFront()
    {
    if (this.size()==0)
    {
    System.out.println("No data to remove.");
    return null;
    }
    else if (this.size()==1)
    {
    Object a = this.head.data;
    this.head = null;
    this.tail = null;
    return a;
    }
    else
    {
    Object a = this.head.data;
    this.head = this.head.next;
    return a;
    }
    }
    Java Code:
    public void insertAtSorted(String s)
    {
    if (this.size()==0)
    {
    insertAtFront(s);
    return;
    }
    else if (s<=this.head)// stuck on these type of statements
    {
    insertAtFront(s);
    return;
    }
    else if (s>=this.tail) // how to make so this works? <----------
    {
    insertAtBack(s);
    return;
    }
    else
    {
    Node ntlnode = this.head;
    while (ntlnode.next <= s)
    {
    ntlnode = ntlnode.next;
    Node blah = new Node(s);
    blah.next = ntlnode;
    this.next = blah;
    }
    }
    }
    Java Code:
    public Object removeFromBack()
    {
    if (this.size()==0)
    {
    System.out.println("No data to remove.");
    return null;
    }
    else if (this.size()==1)
    {
    Object a = this.tail.data;
    this.head = null;
    this.tail = null;
    return a;
    }
    else
    {
    Object a = this.tail.data;
    Node ntlnode = this.head;
    while (ntlnode.next != this.tail)
    {
    ntlnode = ntlnode.next;
    }
    this.tail = ntlnode;
    this.tail.next = null;
    return a;
    }
    }
    public boolean isEmpty()
    {
    return this.head == null;
    }
    public void print()
    {
    if (this.size()==0)
    {
    System.out.println("Empty list!");
    return;
    }
    Node curr = this.head;
    while (curr != null)
    {
    System.out.println(curr.data.toString());
    curr = curr.next; //move forward to the next data item.
    }
    System.out.println();
    }
    public int size()
    {
    int count = 0;
    Node curr = this.head;
    while (curr != null)
    {
    count++;
    curr = curr.next;
    }
    return count;
    }
    }
    The error is:

    Java Code:
    List.java:100: operator <= cannot be applied to java.lang.String,java.lang.Object
    else if (s<=this.head.data) //not sure about this format of object<---------------
    ^
    List.java:105: operator >= cannot be applied to java.lang.String,java.lang.Object
    else if (s>=this.tail.data)
    ^
    List.java:113: operator <= cannot be applied to Node,java.lang.String
    while (ntlnode.next <= s)
    ^
    List.java:118: cannot find symbol
    symbol : variable next
    location: class List
    this.next = blah;
    ^
    4 errors
    Thanks

  2. #2
    barney is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default

    Java Code:
    java.lang.String,java.lang.Object
    else if (s<=this.head.data) //not sure about this format of object<---------------
    Ok, I'm rusty on my Java but I'm pretty sure you can't use the operators on strings. As well, when comparing two things, they must be of the same type. So we can't compare a string to an object. So let's try something else...

    //This converts the Object data to a string and then compares it to s
    Java Code:
    elseif(s.compareTo(this.head.data) <= 0)
    //This does the same as the above to my knowledge, but explicitly casts the Object to a string
    Java Code:
    elseif(s.compareTo((String)this.head.data) <= 0)
    Thus, for the other one, change head to tail and <= to >=
    Greetings.

Similar Threads

  1. [SOLVED] Java Error: Cannot find Symbol...
    By bobleny in forum New To Java
    Replies: 8
    Last Post: 04-15-2008, 06:35 AM
  2. Programm Error: cannot find symbol Help?
    By junix in forum New To Java
    Replies: 2
    Last Post: 12-10-2007, 05:30 AM
  3. cannot find symbol class error
    By po0oker in forum New To Java
    Replies: 5
    Last Post: 10-31-2007, 02:52 PM
  4. Error: cannot find symbol
    By cachi in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 08:12 PM
  5. Error: cannot find symbol constructor
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 08:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •