即使java中的字符串相同,equalsIgnoreCase也返回false

时间:2012-12-28 10:38:13

标签: java string swing

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)
    {

    }

//应该接受CODE的输入...它已被使用.txt文件接受 代码包含

.DATA
 DATAENDS

这个代码应该在点击按钮时执行......

问题:即使值@ tok[0] = .DATA返回false .....已检查tok[]数组,数据正确分配..... 但 条件它仍然给出错误.....

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

1 个答案:

答案 0 :(得分:2)

当您发现equalsequalsIgnoreCase返回false时应该返回true。使用trim进行救援。

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