Java if statement problem...  | |
February 23rd, 2005, 09:37 AM
|
#1 (permalink)
| | Ultimate Member
Join Date: Jan 2003 Location: MA / NH
Posts: 1,497
| Java if statement problem...
This is a very basic if statement in a much longer peice of code:
However, for some reason, it does not work. Can you see a reason why? Code: String strAccess = new String(rights1.getText());
if(strAccess == "1"){
u1.setSelected(true);
u2.setSelected(false);
u3.setSelected(false);
} strAccess will in fact = 1, but the if statement just won't run!
-Blaze |
| |
February 23rd, 2005, 10:00 AM
|
#2 (permalink)
| | Not Really a Member
Join Date: Oct 2001
Posts: 25,396
|
The only thing that comes to mind is datatypes.
Not sure how java does it, it SHOULD treat that 1 as a string which is what you're comparing.. but the only that cames to mind is if its treating it as an integer whereas you'd want to test as strAccess = 1 (sans quotes)
You declared strAccess as a string, so I'd think it would convert whatever gets in as a string, but not sure if it will do that on the fly or if you need to do it explicitly.
__________________
Helicopters don't fly; they vibrate so much and make so much noise that the earth rejects them.
|
| |
February 23rd, 2005, 11:14 AM
|
#3 (permalink)
| | Ultimate Member
Join Date: Jan 2003 Location: MA / NH
Posts: 1,497
|
I rewrote it to use spinners this morning, and got this code, which did the same thing. Code: String strAccess = new String(rights8SpinnerModel.getValue().toString());
if(strAccess == "1"){
o1.setSelected(true);
o2.setSelected(false);
o3.setSelected(false);
} So I musted around for awhile, and wrote this working code: Code: if(rights8SpinnerModel.getValue().toString() == "1"){
o1.setSelected(true);
o2.setSelected(false);
o3.setSelected(false);
} I'm still not sure why the later works and the former doesn't, but it works for now, and for now that's all I need.
-Blaze |
| |
February 23rd, 2005, 11:20 AM
|
#4 (permalink)
| | Ultimate Member
Join Date: Jan 2003 Location: MA / NH
Posts: 1,497
| Quote: |
Originally Posted by vass0922 The only thing that comes to mind is datatypes.
Not sure how java does it, it SHOULD treat that 1 as a string which is what you're comparing.. but the only that cames to mind is if its treating it as an integer whereas you'd want to test as strAccess = 1 (sans quotes)
You declared strAccess as a string, so I'd think it would convert whatever gets in as a string, but not sure if it will do that on the fly or if you need to do it explicitly. | Eclipse is telling me that both are strings, no converting needed. Also, I can output strAccess, or set another text box to equal it or whatever the heck I want to do, but compare it to another string. See above post.
-Blaze |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |