Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incompatible Types - found:int required:boolean

Tags:

java

I'm trying to display: EQUIVALENT if the first numerical input is equal to the second input. What's wrong with my code?

import java.io.*;
public class TwoNum{
    public static void main(String[] args){
        int number;
        int number2;
        String input1="";
        String input2="";

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Input a number: ");

        try{
            input1=in.readLine();
        }catch(IOException e){
            System.out.println("Error!");
        }

        number = Integer.parseInt(input1);

        try{
            input2=in.readLine();
        }catch(IOException e){
            System.out.println("Error!");
        }

        number2 = Integer.parseInt(input2);

        if(number=number2)
        {
            System.out.println("EQUIVALENT");
        }
        if(number>number2)
        {
            System.out.println("GREATER THAN");
        }
    }
}
like image 374
JcDav Avatar asked Mar 12 '26 11:03

JcDav


2 Answers

Use

 if(number==number2)

Instead of

 if(number=number2)

The first compares number2 to number and if they are equal evaluates to true. The second assigns the value of number2 to the variable number and the expression evaluates to number/number2, an int.

Link

  • Summary of Operators (The Java Tutorials)
like image 77
Mark Peters Avatar answered Mar 14 '26 03:03

Mark Peters


The expression number=number2 is an assignment expression producing an integer. But a boolean is expected in this context. You want == instead of =. Common mistake.

like image 39
Ray Toal Avatar answered Mar 14 '26 02:03

Ray Toal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!