Hey again,
I'm having another problem unfortunately. We were asked to create a tree that has binary tree properties as well as max heap property.
(take A(7),C(8) and B(3) ; the tree should be so that the it should alphabetically represent a binary tree and numerically represent a heap)

I tried to create a treap but didn't succeed. so I thought that I'd create a priority queue and sort the items numerically and then remove them one by one and add to the tree, which will sort them in alphabetical order (The best I could come up with).

Now the problem is with the priority queue, it gives me an "operator < cannot be applied to int, java.lang,string " error msg.

public class PriorityQueue {
    
    public String createString(int item, String str) {
        String space, priort;
        priort = Integer.toString(item);
        space = " ";
        totl = str + space + priort;
        return totl;
    }

    public int insertFirst(int item, String keys) {
        String str = createString(item, keys);
        insert(str, item);
    }

    public void insert(String str, int item) {
        int i;

        if (nItems == 0) {
            queArray[nItems++] = str; // insert at 0
        } else {
            for (i = nItems - 1; i >= 0; i--) 
            {
                if (item < queArray[i]) { // the error appears here
                    queArray[i + 1] = queArray[i]; 
                } else {
                   
                    break; 
                }
            }
            queArray[i + 1] = str;
            nItems++;
        }  (nItems > 0)
    }

    public void EmptyQueque() {
        while (!isEmpty()) {
            remove();
        }
    }

    public void remove() {
        if ((!isEmpty())) {
            while (!isEmpty()) {
                String str = queArray[--nItems];
                System.out.println(str + " ");
            }
    }

    public static void main(String[] args) {
        PriorityQueue thePQ = new PriorityQueue(100);
        thePQ.insertFirst(3, "H");
        thePQ.insertFirst(10, "M");
        thePQ.insertFirst(2, "O");
        thePQ.insertFirst(5, "Hfsh");
        thePQ.remove();

    }
}

I wanted to have the createString method because it would be easier for me to just remove items and send it to the tree (is there another method?).
I understand that you can't compare int with the string inside the array but I'm really stuck here and haven't got any ideas.
Is there a way to add a string and integer in to a two-dimensional array or something?
Sorry about the long boring explanation, and thanx for taking the time to read it.
Any help will be much appreciated.

Recommended Answers

All 3 Replies

The reason for that error is not hard to determine: the operator < can only be applied to numeric types. String is a class, a String is an object, they can't be compared in this way. You want the compareTo() method. Read up on Comparable if you're not familiar with it, then use that to do your comparison.

commented: helpfull +2

The reason for that error is not hard to determine: the operator < can only be applied to numeric types. String is a class, a String is an object, they can't be compared in this way. You want the compareTo() method. Read up on Comparable if you're not familiar with it, then use that to do your comparison.

Hey,
Thanx for your suggestion. I did read about Comparables and implemented it in my code. And it works. Thanx most awfully. :)

Glad to help. Best of luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.