• Register
1 vote
57 views

Problem:

Hi! I’m a high school level student and learning Java in class and I have a homework assignment where I have to complete quizzes and exercises online. I need help with a couple of exercises but I solved most of them except one. The problem is to find out the minimum and maximum value by using an If block. But the program is not running and throws back an error: java bad operand types for binary operator. Here is my code snippet

public boolean inRange(int number, int minimum, int maximum){
           if(minimum <= number <= maximum){
              return false;
              }
           else{
              return true;
              }
}

Could any of you please figure out the contagious portion from the program? Thanks.

12 7 7
15,250 points

Please log in or register to answer this question.

2 Answers

1 vote

Solution:

In java, some operators are not companionable. The ( <= ) is one of them. You can’t use the operator in a java program. That’s the reason you are getting the error. So, what should you do now? You need to think of other algorithms and operators to solve the problem. I am giving you a hint of an algorithm below. Please, write the code for your own.

Lets the numbers be a, b, c and 

check if a>b if TRUE {

then check a>c (if it is true then print a as largest) else print c as largest }  

else { b>a 

check if b>c (if it is true then print b as largest) else print c as largest) }.

 

13 9 6
94,240 points
0 votes

Solution:

== has higher precedence than &. You might want to wrap your operations in () to seclude how you want your operands to bind to the operators.

((a[0] & 1) == 0)

Likewis for all parts of the if condition.

Since & has a lesser priority than ==.

Your code is equivalent to a[0] & (1 == 0), and if not a[0] is a boolean this won't compile…

You require to:

(a[0] & 1) == 0

or You can attempt this way

The error message is pretty clear: you can't liken an Object with an int.
In case you are certain the low1 collection really conceive ints, then cast it properly, for example

int x = (int)low1.get(a);
for (int i = 0; i < row; i++) {
  if (dc[i][a] == x) {
    i1.add(i);
  }
}

By the way it would better applying the generic collections (that is ArrayList<E>[^]).

10 6 4
31,120 points

Related questions

0 votes
1 answer 15 views
15 views
Problem: [Fixed] bad operand types for binary operator in java. public static void main(String[] args) { System. out. println(evenOdd(2)); } static int evenOdd (int num) { return (num&1==0)? 0:1; // error: bad operand types for binary operator '&'
asked Feb 13 Ali 5.7k points
0 votes
1 answer 11 views
11 views
Problem: I've fixed the code: if ((a[0] & 1) == 0 && (a[1] & 1) == 0 && (a[2] & 1) == 0){ Was an issue with the brackets, updated for anyone in the future be more precise,i using parentheses, otherwise Java will not use the order of operands that i want it to use.
asked Feb 13 Ali 5.7k points
0 votes
1 answer 10 views
10 views
Problem: I have to be more precise, using parentheses, otherwise Java will not use the order of operands that you want it to use. For example, + is an operator used for addition, while * is also an operator used for multiplication. classified objects in java,
asked Feb 14 Ali 5.7k points
0 votes
1 answer 11 views
11 views
Fix it.
asked Feb 6 TeamScript 13.5k points
0 votes
1 answer 10 views
10 views
I want to end the loop after the correct input. Help me to solve this problem.
asked Dec 16, 2020 TeamScript 13.5k points
0 votes
1 answer 10 views
10 views
I have a Java project I am working on and I get this error. I was working on that. I just want to know how to do that?
asked Dec 3, 2020 TeamScript 13.5k points
0 votes
1 answer 16 views
16 views
Problem: I've fixed the code: if ((a[0] & 1) == 0 && (a[1] & 1) == 0 && (a[2] & 1) == 0){ Was an issue with the brackets, updated for anyone in the future. The error shows this line if ((a[0] & 1 == 0) && (a[1] & 1== 0) && (a[2] & 1== 0)){
asked Feb 3 Ali 5.7k points
0 votes
0 answers 4 views
4 views asked Feb 23 Hira 2.2k points
0 votes
0 answers 26 views
26 views
I just made a new django project, pushed it to git and now I am trying to go deploy it using Digital ocean. I installed all the needful things like pip, python etc. but I am not able to make the python manage.py runserver work. ((g-v) is my virtualenv so that is not the problem.) ... 0 singledispatch 3.4.0.3 six 1.12.0 typing 3.7.4 virtualenv 16.7.3 virtualenv-clone 0.5.3 wheel 0.33.6 wrapt 1.11.2
asked Oct 27, 2020 psandprop 2.4k points
0 votes
1 answer 129 views
129 views
I want to know how to trace the count of 1 in the binary representation.
asked Sep 17, 2020 Daniel Anderson 4k points