Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem using if and else if

 
Tavo Gtz
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Screenshot from 2013-04-19 17:28:24.png
[Thumbnail for Screenshot from 2013-04-19 17:28:24.png]
This is the screen Im using
 
Tavo Gtz
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If somebody has a better way of doing this, please enlighten me
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
Tavo Gtz
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Jeff, it helped me a lot.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tavo Gtz wrote:thank you Jeff, it helped me a lot.


You're welcome!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic