|
-
Oct 29th, 2002, 09:14 PM
#1
Thread Starter
Hyperactive Member
String?
Ok i remember my teacher saying something about useing a string in a if but i dont remember what it is :-/
this isnt working ???? Why?
Code:
if(strName != " ")
{
lblName.setText(strName);
}
else
{
//Do Something
lblError.setText("Error1");
}
-
Oct 29th, 2002, 10:59 PM
#2
When doing Object comparisions, the normal equality operators(==, !=, etc) will only compare whether or not the variables point to the same object. To actually compare values, use the equals() method.
Code:
if(!strName.equals(" "))
{
lblName.setText(strName);
}
else
{
//Do Something
lblError.setText("Error1");
}
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Oct 30th, 2002, 12:09 AM
#3
Thread Starter
Hyperactive Member
-
Oct 30th, 2002, 12:34 AM
#4
Thread Starter
Hyperactive Member
hmm that still didnt work :-/
-
Oct 30th, 2002, 07:19 AM
#5
Are you getting errors, or are the results just not as you expected?
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Oct 30th, 2002, 11:03 AM
#6
Addicted Member
I can't see how this has any errors in it. I've always used the String.equals("") method.
Does this throw an exception when the string inside the equals() is longer than the string itself? e.g.
Code:
String thisString = "";
if(!thisString.equals(" "))
{
....
}
This might throw an exception..?
HD
-
Oct 30th, 2002, 12:51 PM
#7
Would be very strange if it did, I would consider it a bug.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 30th, 2002, 04:27 PM
#8
Thread Starter
Hyperactive Member
im gettting a single result if its blank its still displaying the true result
-
Oct 31st, 2002, 12:38 AM
#9
Thread Starter
Hyperactive Member
heres my Code just to give a ref
Code:
public void Check()
{
strName = txtName.getText();
strAddress = txtAddress.getText();
strCC = txtCC.getText();
int intQuantity = Integer.parseInt(txtQuantity.getText());
if(!strName.equals(" "))
{ //Validation myVal = new Validation(intQuantity);
lblTitle.setText("Hello");
}
else
{
lblTitle.setText("GoodBy");
}
}
-
Oct 31st, 2002, 07:03 AM
#10
Are you looking for a string that is a single space, or an empty string? Because your code is looking for a string that is a single space. Try
Code:
if (strName.length() > 0)
{
lblTitle.setText("Hello");
}
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Oct 31st, 2002, 09:41 AM
#11
Addicted Member
Posted by CornedBee
Would be very strange if it did, I would consider it a bug.
Sorry, I was thinking of String.substring() which throws an exception when you do something like:
Code:
String str = "hello";
if(str.substring(0, 7).equals("goodbye"))
{
.......
}
Thats what I was confusing it with. Obviously this would cause an exception as its trying to access string indexes that dont exist.
HD
-
Oct 31st, 2002, 01:19 PM
#12
Thread Starter
Hyperactive Member
Ok it Works now thanks alot!!
Last edited by 308holes; Oct 31st, 2002 at 01:23 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|