Benjamin Varghese
Benjamin Varghese

Reputation: 103

equalsIgnoreCase returns false even when strings are same in java

int locctr = 0,toknum = 0;

    String temp;
    String d;
    String [] tok = new String[500];
    String[] lines = CODE.getText().split("\n");
    try
    {
        for(int i=0;i<lines.length;i++)
        {
            temp = lines[i];
            if(temp != null)
            {
                StringTokenizer st = new StringTokenizer(lines[i],":,\t[]",true); 
                while (st.hasMoreTokens())
        {
                    String ss=st.nextToken();
                    tok[toknum] = ss;  
                    toknum++; //increment
                }
            }
        }
        System.out.print("tok[0] is "+tok[0]);
        boolean r = tok[0].equalsIgnoreCase(".DATA");
        System.out.print("r is : "+r);
        if(r == true)
        {
           System.out.print("\n      INSIDE .DATA"); 
        }
    }
    catch(Exception e)
    {

    }

// input should be accepted for CODE ...its been accepted using a .txt file CODE contains

.DATA
 DATAENDS

this code should be executed on button click......

PROBLEM : even when the value @ tok[0] = .DATA its returning false.....have checked the tok[] array to the data is getting assigned correctly..... but condition it still gives false.....

OUTPUT :

     temp is .DATA
         SS IS .DATA
         tok[num] .DATA
         toknum 0
     temp is DATAENDS
         SS IS DATAENDS
         tok[num] DATAENDS
         toknum 1tok[0] is .DATA
    r is : false

Upvotes: 0

Views: 2628

Answers (1)

Azodious
Azodious

Reputation: 13872

When you find equals or equalsIgnoreCase is returning false when it should has return true. use trim to rescue.

if(tok[0] != null && tok[0].trim().equalsIgnoreCase(".DATA"))

Upvotes: 2

Related Questions