Kcque
Kcque

Reputation: 1

how to check if there is no text in the textfield so that error message can b displayed

if(namefield.getText().compareTo("")==0)  

Is this code above correct to check that no input is there in the textfield so that a error message can be generated? provided have to use compareTo func only

Upvotes: 0

Views: 720

Answers (4)

Chuck
Chuck

Reputation: 76

It depends on how the namefield.getText() method is implemented in your platform. Since you didn't method which platform you are using. I suggest you can check the documentation.

Generally, when the namefield is not set, namefield.getText() will return a empty String which is "". So we don't need to check if it's null.

So we can check using following code:

if(namefield.getText().isEmpty()){}

which is same as following:

if(namefield.getText().length()==0){}

Because String.isEmpty() method is implemented as following:

public boolean isEmpty() {
     return value.length == 0;
}

On the other hand, when the namefield.getText() can return null. You need to check null to avoid NPE.

String name = namefield.getText();
if(name==null || name.isEmpty()){}

Finally, if you want to check if the input string is whitespace, you can simply use String.trim() to remove the whitespace.

if(namefield.getText().trim().isEmpty()){}

or

String name = namefield.getText();
if(name==null || name.trim().isEmpty()){}

Even though we can use name.equals(""), I don't think it's the best way. Since in String.equals method, it firstly check if the objects are the same and then use the length to check if they're equal. However, when we get a text from a Textfield, the object will not be the same with the constant string "".

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = value.length;
        if (n == anotherString.value.length) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = 0;
            while (n-- != 0) {
                if (v1[i] != v2[i])
                    return false;
                i++;
            }
            return true;
        }
    }
    return false;
}

Upvotes: 1

Michele Da Ros
Michele Da Ros

Reputation: 906

I would use

if((namefield.getText()== null) || (namefield.getText().length() == 0)){
  show error message
}

Upvotes: 0

Harmantj
Harmantj

Reputation: 182

The method compareTo() is used for comparing two strings lexicographically. Each character of both the strings is converted into a Unicode value for comparison. If both the strings are equal then this method returns 0 else it returns positive or negative value. The result is positive if the first string is lexicographically greater than the second string else the result would be negative.

Instead use equals()

 if(namefield.getText().equals(null) || namefield.getText().equals(""))
    {
    //do this
    }

Upvotes: 0

Anas
Anas

Reputation: 678

to be more accurate

String data = nameField.getText()
if(data==null || data.length()==0)
{
//show error message here
}

Upvotes: 1

Related Questions