# Problem using if and else if

Tavo Gtz

Greenhorn

Posts: 8

posted 2 years ago

- 0

Hey guys, Im getting an error, but I just dont see why. Anyone able to help? Im pretty sure its a basic issue, Im just not able to see it.

Im doing a calculator that can convert numeric systems, from binary to decimal, octal, hexadecimal. From decimal to binary, octal, hexadecimal and so all the other combinations.

I believe my problem is in my logical operators.

This is my code so far:

private javax.swing.JTextField Convert; //textfield where I enter the number I want to convert

private javax.swing.JTextField Result;

Res = Integer.parseInt(Convert.getText());

if(Decimal1.isSelected() && Binary2.isSelected())

{Result.setText(Integer.toBinaryString(Res));}

if(Decimal1.isSelected() && Octal2.isSelected())

{Result.setText(Integer.toOctalString(Res));}

else(Decimal1.isSelected() && Hexa2.isSelected())

{Result.setText(Integer.toHexString(Res));}

my radio button group in the left is where Decimal1 exists and on the right Binary2, Octal2, Hexa2

Im doing a calculator that can convert numeric systems, from binary to decimal, octal, hexadecimal. From decimal to binary, octal, hexadecimal and so all the other combinations.

I believe my problem is in my logical operators.

This is my code so far:

private javax.swing.JTextField Convert; //textfield where I enter the number I want to convert

private javax.swing.JTextField Result;

Res = Integer.parseInt(Convert.getText());

if(Decimal1.isSelected() && Binary2.isSelected())

{Result.setText(Integer.toBinaryString(Res));}

if(Decimal1.isSelected() && Octal2.isSelected())

{Result.setText(Integer.toOctalString(Res));}

else(Decimal1.isSelected() && Hexa2.isSelected())

{Result.setText(Integer.toHexString(Res));}

my radio button group in the left is where Decimal1 exists and on the right Binary2, Octal2, Hexa2

Tavo Gtz

Greenhorn

Posts: 8

posted 2 years ago

- 0

If you have 3 mutually exclusive conditions (exactly one of them will be true, no more, no less), then you want this:

However, if it's possible for NONE of them to be true (but still at most one can be true) then you want:

Make sure you understand the difference between

And finally, note that we cannot do

However, if it's possible for NONE of them to be true (but still at most one can be true) then you want:

Make sure you understand the difference between

And finally, note that we cannot do

It is sorta covered in the JavaRanch Style Guide. |