user2992500 user2992500 - 1 month ago 6
Java Question

"first type int, second type boolean" java

so I'm just playing around with some stuff I learnt, making some pointless calculator. Before anyone says, I know this could be done much more simply!

I'm getting this error though on my while statement (bad operand types for binary operator '||' first type: int, second type: boolean")

it's also throwing problems with the

int AA = A + B;
int AB = A * B;
int AC = A / B;


saying cannot find symbol.

class cases{ //1-4 sums with cases, delete after//
public static void main(String args[]){

System.out.println("Welcome to a pointless calculator.");

int A = Integer.parseInt(args[0]);
int B = Integer.parseInt(args[1]);
switch (A){
case 1:
System.out.print("You entered 1");
break;
case 2:
System.out.print("You entered 2");
break;
case 3:
System.out.print("You entered 3");
break;
case 4:
System.out.print("You entered 4");
}
while ( A || B > 4){
System.out.print("please enter numbers 1-4");
break;


}
switch (B) {
case 1:
System.out.println(" and 1");
break;
case 2:
System.out.println(" and 2");
break;
case 3:
System.out.println(" and 3");
break;
case 4:
System.out.println(" and 4");
}

}
{

int AA = A + B;
int AB = A * B;
int AC = A / B;


System.out.print("the answers added = ");
System.out.println(AA);
System.out.print("the answers multipled = ");
System.out.println(AB);
System.out.print("the answers divided = ");
System.out.println(AC);



}
}

Answer

The problem is here:

while ( A || B > 4){

That expression breaks down like this:

while (
    A
    ||
    B > 4
){

A is of type int, but you're treating it like a boolean. You can't do that in Java. You may have meant:

while ( A > 4 || B > 4){

There may well be further problems in the code. For instance, you have a block not associated with anything at the end of your code. I think in Java that ends up being an instance initializer block, but frankly I think you need to step back and work through some tutorials.