0

The following method replaces each parenthesis and each comma in a string variable with a space. It also replaces multiple spaces with only one space using a regular expression.

// -----------------------------------------------------
//  Replace special characters by spaces.
// -----------------------------------------------------
private String ParseCommand(String Command)
{
    String strCommand = Command;

    // Replace parenthesis and commas by a space.
    strCommand = strCommand.replace('(', ' ');
    strCommand = strCommand.replace(')', ' ');
    strCommand = strCommand.replace(',', ' ');

    // Remove extra spaces.
    strCommand = strCommand.replaceAll("\\s+"," ");
    return strCommand;
}

Above method "ParseCommand" is called by method "SplitAndFind" which splits a string based on a space. Also, it searches for a token in the resulting array

// -----------------------------------------------------
//  Find a token in command.
// -----------------------------------------------------
public void SplitAndFind(String Command, String TokenToFind)
{
    String strCommand = ParseCommand(Command);
    String[] strTokens = strCommand.split(" ");
    for (int i = 0; i <= strTokens.length - 1; i++)
    {
        System.out.println(strTokens[i]);
        if (strTokens[i] == TokenToFind)
        {
            System.out.println("TOKEN FOUND !!!");
        }
    }
}

Finally I call method SplitAndFind from main looking for the token PRIMARY. My problem is that the token is not found. I display every item in the array of tokens and I see it but the message "TOKEN FOUND !!!" is never displayed. What am I doing wrong?

public static void main(String[] args) throws FileNotFoundException,     IOException
{
    dbEngine objEngine = new dbEngine();
    objEngine.SplitAndFind("CREATE TABLE animals (PRIMARY VARCHAR(20), kind VARCHAR(8), years INTEGER) PRIMARY KEY (name, kind);", "PRIMARY");
}
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
JORGE
  • 167
  • 1
  • 3
  • 12

1 Answers1

2

Strings need to be compared with the equals function. So, this line of code:

if (strTokens[i] == TokenToFind)

should be this:

if (strTokens[i].equals(TokenToFind))
TangledUpInBlue
  • 746
  • 4
  • 13
  • Yes, I have already solved my issue. This question was marked as "duplicate" so I saw the other question and found the answer. Thank you for your time. – JORGE Feb 12 '16 at 16:56