home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

Java if statement problem...

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 2776
Discussions: 200,970, Posts: 2,379,706, Members: 246,334
Old February 23rd, 2005, 09:37 AM   Digg it!   #1 (permalink)
Ultimate Member
 
Join Date: Jan 2003
Location: MA / NH
Posts: 1,497
Send a message via AIM to Blazer06
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
Blazer06 is offline   Reply With Quote
Old 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.
vass0922 is online now   Reply With Quote
Old February 23rd, 2005, 11:14 AM     #3 (permalink)
Ultimate Member
 
Join Date: Jan 2003
Location: MA / NH
Posts: 1,497
Send a message via AIM to Blazer06
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
Blazer06 is offline   Reply With Quote
Old February 23rd, 2005, 11:20 AM     #4 (permalink)
Ultimate Member
 
Join Date: Jan 2003
Location: MA / NH
Posts: 1,497
Send a message via AIM to Blazer06
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
Blazer06 is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
java virtual machine jervic Applications and Operating Systems 3 October 20th, 2004 11:20 AM
where to get microsoft virtual machine? dustinbob General Tech Discussion 23 March 1st, 2004 07:33 PM
java trouble dezzzi28 Technical Support 7 September 24th, 2002 09:33 PM
WinXP Pro and Javascript question bill1971 Applications and Operating Systems 6 August 31st, 2002 04:06 AM
can't update windows?? need som help please zskillz Technical Support 7 August 4th, 2002 05:16 AM


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (3035)
Forty-six years ago today (12)
The disrespect of Obama by Russian .. (47)
Laptop with wireless problem. (12)
Wireless Televisions. (12)
CPU fan stops spinning randomly (11)
Regular Build (11)
windows vista security holes (17)
Internet Lost (5)
windows 7 problem (7)
Is the PSU I received dead? (13)
Point and Shoot Camera Suggestions. (6)
radeon x850xt platinum & shader.. (6)
HIS HD5770 graphic card question (15)
Recent Discussions
Nvidia GTX 260 problem (4)
Steam ID's, Gamertags etc... (2)
Games, Cables, PCI cards, and more fo.. (6)
Dept. of HS: NSA 'Helped' Develop Vis.. (17)
Delete an OS (9)
Linksys WMP54GS wireless card problem.. (5)
windows vista security holes (17)
Help getting around port 80 for camer.. (5)
Skillsoft Network+ Study Software Que.. (10)
Browsers wont load websites (3)
help me pls laptop just stopped worki.. (0)
Open With ..... Win7 (3)
Laptop with wireless problem. (12)
Internet Lost (5)
virus blocking exe. files (1)
Point and Shoot Camera Suggestions. (6)
CPU fan stops spinning randomly (11)
Modern Warfare 2: Who Bought It? (65)
Is the PSU I received dead? (13)
Print spooler problem (16)
Kingston Bluetooth Dongle Driver (1)
Multiple Restarts Required at Boot (3)
webcam (0)
upgrade for hp a6101 (0)
tv not turn on-makes clicking sound (2)


All times are GMT -4. The time now is 05:44 PM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28