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");
}
Printable View
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");
}
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");
}
Thanks Much
hmm that still didnt work :-/
Are you getting errors, or are the results just not as you expected?
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.
This might throw an exception..?Code:String thisString = "";
if(!thisString.equals(" "))
{
....
}
HD
Would be very strange if it did, I would consider it a bug.
im gettting a single result if its blank its still displaying the true result
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");
}
}
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");
}
Sorry, I was thinking of String.substring() which throws an exception when you do something like:Quote:
Posted by CornedBee
Would be very strange if it did, I would consider it a bug.
Thats what I was confusing it with. Obviously this would cause an exception as its trying to access string indexes that dont exist.Code:String str = "hello";
if(str.substring(0, 7).equals("goodbye"))
{
.......
}
HD
Ok it Works now thanks alot!!