Piyush Saxena Piyush Saxena - 6 months ago 20
Java Question

Program produces wrong Output

Having trouble with the below program. I am basically novice in java right now.
The code looks like below :-

public class IfElseIf{

public static void main(String []args)
{
int date=26;
int month=9;
int year=1995;
if ((date>=1 | date <=10) || (month>=1 | month <=4) || year==1995)
{
System.out.println("Your bday is in between 1-10/1-4/1995");

}
else if((date>=11 | date <=20) || (month >=5 | month <=8) || year==1995)
{
System.out.println("Your bday is in between 11-20/5-8/1995");
}
else if((date>=21 | date<=31) || (month >=9 | month <=12) || year==1995)
{
System.out.println("Your bday is in between 21-31/9-12/1995");
}
else
{
System.out.println("Your bday not found");
}
}
}


On running the above, it gives me below as the output :-
Your bday is in between 1-10/1-4/1995
Anyone, please suggest what might be possibly wrong here?

Answer

You're using the wrong operators; | is bitwise "OR", and you want logical "AND", which is &&.

Comments