2

I have loaded the contents of the database in an ArrayList named countList. The contents loaded are of int type. I created countList using the command

ArrayList countList = new ArrayList();

Now, I need to check if each contents of the arraylist is greater than three. I wrote it like

for(int i=0; i< itemset.size(); i++){
    if(countList.get(i) >= 3)
    {

    }
}

When I write it simply, it shows error of bad operand type for binary operator '>='. How to do the task?

4
  • 2
    What is supportcount and itemset in your code? Commented Jun 6, 2013 at 1:52
  • 1
    Where have you declared supportcount and i? If you have done the declaration correctly, there is not reason why this shouldn't work. Commented Jun 6, 2013 at 1:53
  • i am sry for errors in the previous post. i have edited the code. itemset is also an arraylist. And i have loaded the two fields product_no and quantity of my database into itemset and countList. Commented Jun 6, 2013 at 1:57
  • Print out some items to standard output. See if they are numbers. Commented Jun 6, 2013 at 2:01

2 Answers 2

4

The >= operator is only defined on number types such as int, double or Integer, Double. Now, countlist may well contain integers (I assume it does), but the way you have written your code, the compiler can't be sure. This is because an ArrayList can store any type of object, including but not necessarily Integer. There are a couple of ways you can remedy this:

a) You can cast the ArrayList item to an Integer, at which point the >= operator will work:

if ( (Integer) countList.get(i) >= 3)

b) You can use generics to tell the compiler that your ArrayList will ONLY store Integers:

ArrayList<Integer> countList = new ArrayList<Integer>();
Sign up to request clarification or add additional context in comments.

Comments

-1
for(i=0; i< itemset.size(); i++){
   if (itemset.get(i) > 3) {
      // Do whatever you want here
   }
}

1 Comment

it does not work either. The above post is correct. I used generics as jazzbassrob suggested and it absolutely works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.