• Post Reply Bookmark Topic Watch Topic
  • New Topic

Bad operand for binary operator '%'  RSS feed

 
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have tried searching the web all over for nearly five hours, I tried solving the errors on my own but no luck.

 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s is an ArrayList. You can't use the % operator on an ArrayList. What do you expect that an expression like "s % 3" means?

Did you mean i % 3 and i % 5 instead of s % 3 and s % 5?
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only operands you can use for the % remainder operator are primitives which represent numbers. If you use an instance of one of the 7 “wrapper” Number classes, that will be unboxed to a primitive. That applies both to the left and the right operands. The integer value 0 is not permissible on the right.

For the full details see the Java® Language Specification, but that may be difficult to read.
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Did you mean i % 3 and i % 5 instead of s % 3 and s % 5?
Quite yes.
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think i need to convert it to an array or use a get() method.
 
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ray Anderson wrote:I think i need to convert it to an array or use a get() method.
Convert, no. get(), yes. You are attempting to use '%' on a list. You need to operated on an element in the list.
 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note here that you may ensure that capacity before going into the loop is 1000, and your loop counter goes to 1000, but as you remove() items the size will be less than 1000.
 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Note here that you may ensure that capacity before going into the loop is 1000, and your loop counter goes to 1000, but as you remove() items the size will be less than 1000.
Additionally, when you remove(i) the remaining items will be shifted left to fill the gap, then when you i++ in the loop you'll be skipping over one.
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Carey, Going to try your suggestions.
 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your for(), instead of comparing to a constant 1000, compare to s.size(). This will handle the shrinking size properly.
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for clearing the errors, rest I will solve it on my own.
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yay I solved it, using ArrayList.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!