字符串连接问题。输出仍然是用于初始化的空字符串

时间:2014-08-03 12:07:38

标签: java string-concatenation

我遇到字符串连接问题。输出仍然是初始化的空字符串:

String ct=" ";//Encrpion part of playfair cipher
static void encryption()// The string ct is not getting printed. Only the empty string with which it was initialised is getting printed
{
    int row=98,col=7,row1=9,col1=24;//Just to initialise the variables, random numbers are assigned
    boolean bool= false, bool1=false;
    for(int i=0;i<l-1;i++)
    {
        bool=false;//So that only when both the bool values are true, it tells that it is of that iteration
        bool1=false;
        String a=Character.toString(pt.charAt(i));
        String b=Character.toString(pt.charAt(i+1));
        for(int j=0;j<5;j++)
        {
            for(int k=0;k<5;k++)
            {
                if(a==kmatrix[j][k])
                {
                    row=j;
                    col=k;
                    bool=true;
                }
                if(b==kmatrix[j][k])
                {
                    row1=j;
                    col1=k;
                    bool1=true;
                }
                while((bool==true)&&(bool1==true))
                {
                    if(col==col1)
                    {
                        if(Math.abs(row-row1)==1)//To get the difference 
                        {
                            if(row<row1)
                            {
                                j=row1+1;
                                ct+=kmatrix[j][col];
                                ct+=kmatrix[row1][col];
                            }
                            else
                            {
                                j=row+1;
                                ct+=kmatrix[j][col];
                                ct+=kmatrix[row][col];
                            }

                        }
                    }
                    if(row==row1)
                    {
                        if(Math.abs(col-col1)==1) 
                        {
                            if(col<col1)
                            {
                                k=col1+1;
                                ct+=kmatrix[row][col1];
                                ct+=kmatrix[row][j];
                            }
                            else
                            {
                                k=col+1;
                                ct+=kmatrix[row][col];
                                ct+=kmatrix[row1][k];
                            }

                        }
                    }
                    if((row!=row1)&(col!=col1))
                    {
                        if(row<row1)
                        {
                            ct+=kmatrix[row][col1];
                            ct+=kmatrix[row1][col];
                        }
                        else
                        {
                            ct+=kmatrix[row1][col];
                            ct+=kmatrix[row][col1];

                        }

                    }
                }
            }
        }
        i=i+1;     
    }
    System.out.println(ct);
}

2 个答案:

答案 0 :(得分:0)

我在代码中看到的第一个问题是你将两个字符串与==运算符进行比较,这对于代码来说是不正确的,并且它的计算结果为false。此运算符不比较两个字符串的值,它只检查两个引用是否引用同一个对象。

if(a==kmatrix[j][k]) {
if(b==kmatrix[j][k]) {

尝试将字符串与 equals equalsIgnoreCase 方法进行比较(第二种方法不区分大小写):

if(a.equalsIgnoreCase(kmatrix[j][k])) {
if(b.equalsIgnoreCase(kmatrix[j][k])) {

答案 1 :(得分:0)

我认为matrixGeneration()方法应该存在一些问题,因为生成的矩阵的值在评估导致连接的条件时起着至关重要的作用。 您能否发布代码ot matrixGeneration()方法?