This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Problem using if and else if Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem using if and else if" Watch "Problem using if and else if" New topic
Author

Problem using if and else if

Tavo Gtz
Greenhorn

Joined: Mar 29, 2013
Posts: 8
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



[Thumbnail for Screenshot from 2013-04-19 17:28:24.png]

Tavo Gtz
Greenhorn

Joined: Mar 29, 2013
Posts: 8
If somebody has a better way of doing this, please enlighten me
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Mar 29, 2013
Posts: 8
thank you Jeff, it helped me a lot.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Tavo Gtz wrote:thank you Jeff, it helped me a lot.


You're welcome!
 
jQuery in Action, 2nd edition
 
subject: Problem using if and else if
 
Similar Threads
Help with radio button action depending of another radio button in another group
converting to binary format
please help ???? how to convert hexadecimal ,octol to decimal values and back
Binar representation of octal & hexadecimal nos
Numerice value for letter 'a'