Reputation: 21
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");
}
}
}
Upvotes: 2
Views: 18764
Reputation: 59694
Your first condition should be:
if(number==number2)
In if condition use ==
to compare 2 integers. Also don't use if in both condition use else if()
. Using if
in both will check condition for both even though first condition is true it will check for second condition and you are missing 3rd condition for LESS THAN
.
Upvotes: 1
Reputation: 88478
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.
Upvotes: 2
Reputation: 81164
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.
Upvotes: 10