2016-02-12 1 views
0

La méthode suivante remplace chaque parenthèse et chaque virgule d'une variable de chaîne par un espace. Il remplace également plusieurs espaces avec un seul espace en utilisant une expression régulière.Impossible de trouver une erreur de logique dans mon code simple

// ----------------------------------------------------- 
// 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; 
} 

méthode ci-dessus « ParseCommand » est appelée par la méthode « SplitAndFind » qui divise une chaîne en fonction d'un espace. En outre, il recherche un jeton dans le tableau résultant

// ----------------------------------------------------- 
// 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 !!!"); 
     } 
    } 
} 

Enfin, j'appeler la méthode SplitAndFind de main à la recherche du PRIMARY jeton. Mon problème est que le jeton n'est pas trouvé. J'affiche chaque objet dans la matrice de jetons et je le vois mais le message "JETON TROUVE !!!" n'est jamais affiché. Qu'est-ce que je fais mal?

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"); 
} 
+2

Sa 'si (strTokens [i] equals (TokenToFind))' – Satya

Répondre

2

Les chaînes doivent être comparées à la fonction égale. Ainsi, cette ligne de code:

if (strTokens[i] == TokenToFind) 

devrait être ceci:

if (strTokens[i].equals(TokenToFind)) 
+0

Oui, je l'ai déjà résolu mon problème. Cette question a été marquée comme "doublon" donc j'ai vu l'autre question et trouvé la réponse. Merci pour votre temps. – JORGE