October 23rd, 2002, 03:52 AM
|
#1 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Pasadena, CA
Posts: 2,152
| Converting/reading int as a boolean...
I have this set of code in my main RentACar.class: Code: int aaa = JOptionPane.showConfirmDialog(null,"AAA Selected?"); This'll show a dialog box with the text AAA Selected? and then a 'yes' 'no' 'cancel' button. It returns 0, 1, and 2 respectively. The project Im working on requires that we store the instance variable 'aaa' (located in the CarRental.class) as a boolean. When I use a set method to set a value to 'aaa' it obviously needs to be a boolean but showConfirmDialog returns an int. So any ideas on how I should go about converting that int into a boolean first...or maybe even changing things around so it would work better.
This teacher is extremely anal and is making some projects more difficult than they need to be....such is school life!!! 
__________________
YAH! I knew you'd be jealous
|
| |
October 23rd, 2002, 03:59 AM
|
#2 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 21,017
|
you could use a switch statement
case 1:
boolvar = true;
case 2:
boolvar = false;
case 3:
break;
that work? |
| |
October 23rd, 2002, 04:01 AM
|
#3 (permalink)
| | Real gangstas sip on Yacc
Join Date: Oct 2001 Location: Suckas-ville
Posts: 4,549
|
Hmmm, if bool is a native type, can you cast it?
Dunno how well that would work, but worth a try
Jkrohn
__________________
Signatures blow hard
If your signature contains an ad of any kind, congratulations, you're on my ignore list.
|
| |
October 23rd, 2002, 05:28 AM
|
#4 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 1,542
|
vass, Java switch statements 'fall through' (like in C)
jkrohn, Java only lets you cast primitive numeric types to each other - boolean is primitive but not numeric.
Tekk, how's about : Code: int result = JOptionPane.showConfirmDialog(null,"AAA Selected?");
if (result == JOptionPane.CANCEL_OPTION || result == JOptionPane.CLOSED_OPTION) {
// bottle out
} else {
boolean bresult = (result == JOptionPane.YES_OPTION);
} |
| |
October 23rd, 2002, 10:45 PM
|
#5 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Pasadena, CA
Posts: 2,152
|
works fantastic stranger  Altho I suppose I could made this easier and instead of using showConfirmDialog I could have done showInputDialog and had the user enter a y or an n. Then convert those to a boolean. Ah well. This way is more fancy!!! Thanks again |
| |
October 24th, 2002, 12:43 PM
|
#6 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 1,542
|
Yeah, don't annoy your users by making them do more work than they need to!  |
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |