0

I have a code here that changes the color of a word from a sentence. Violet if the word found is of the same position. Yellow if the answer contains a word but of a different position and red if the word is not found.

My problem right now is that the color changes to violet even though the word is of different position. I also tried to use splitInput[i].contains(splitAnswer[i]) to change the word to yellow but i got a repeated words for example "It was a a sample sample sentence sentence".

        String answer = "This is a sample sentence"
        String userInput = "It was a sample sentence"
        boolean wordFound = false;
        String[] splitAnswer = answer.split(" ");
        String[] splitInput = userInput.split(" ");

        for (int i=0; i<splitAnswer.length;i++) 
        {
            for (int j=0;j<splitInput.length;j++) 
            {
                if(splitInput[i].equalsIgnoreCase(splitAnswer[i])) 
                {
                wordFound = true;
                //color the word to violet
                }
            {
            if(wordFound==false)
            {
            //color the word to red 
            }
             //display the sentence
             wordFound == false; 
        }
Mark
  • 45
  • 6

1 Answers1

0
String answer = "This is a sample sentence";
        String userInput = "It was a sample sentence";
        boolean wordFound = false;
        String[] splitAnswer = answer.split(" ");
        String[] splitInput = userInput.split(" ");

         for (int i=0; i<splitAnswer.length;i++) 
         {

        if (splitInput[i].equalsIgnoreCase(splitAnswer[i]))
        {

            System.out.println ("Word found");
        }
        else if(!wordFound)
        {

            System.out.println ("Word Not found");
        }
    }
PPD
  • 5,660
  • 12
  • 52
  • 86
  • Hello!sorry for the late response. I tried that code last time and I get a null pointer exception error message. By the way the userInput is not constant and the array length might change based on the user input. Can you help me how to resolve this? – Mark Jan 19 '15 at 09:18