title: Compare two strings and highlight the mismatch wherever found
body: <p>I want to compare two strings and highlight the word(s) wherever there is a mismatch.</p>
  
  <p>The code I have written has the following 2 issues : </p>
  
  <p>1 . Wherever there is a mismatch, the word gets stored in a 'token' but as soon as the highlight function gets called, it highlights all the words in that string which matches the word in the token.</p>
  
  <p>2 . All the differences gets displayed on the console. However by the time the GUI loads, only the last value in the array index is highlighted thereby not showing the previous highlights.</p>
  
  <p>Here is the Code so far. Also I have attached the snapshot for reference. Any suggestions would be of great help. Thanks.:</p>
  
  <pre><code>package com.check;
  
  import java.lang.reflect.InvocationTargetException;
  import javax.swing.*;
  import javax.swing.text.*;
  import java.awt.*;
  
  public class highlightCheck {
  
  int i, j, x;
  
  int Flag;
    int length;
   //int errorIndex;
  
  int errorIndex[] = new int[2];
  int loop;
  
  String s1 = "";
  String s2 = "";
  
  String Retext1 = "Extreme programming is one approach of agile software development which emphasizes on frequent releases on short development cycles which are called time boxes. This result in reducing the costs spend for changes, by having multiple short development cycles, rather than one long one. Extreme programming includes pair-wise programming (for code review, unit testing).Also it avoids implementing features which are not included in the current time box, so the schedule creep can be minimized.";
  String Retext2 = "Extreme programming is one approach of agile software development which emphasizes on frequent releases in short development cycles which are called time boxes. This result in reducing the costs spend for changes, by having multiple short development cycles, rather than one long one. Extreme programming includes pair-wise programming (for code review, unit testing).Also it avoids implementing features which are not included in the current time box, so the schedule creep can be minimized.";
  
  
  String token1;
  String token2;
  
  public static void main(String args[]) {
  
      try {
  
          SwingUtilities.invokeAndWait(new Runnable() {
  
              public void run() {
                  new highlightCheck().createAndShowGUI();
              }
          });
  
      } catch (InterruptedException ex) {
          ex.printStackTrace();
  
      } catch (InvocationTargetException ex) {
          ex.printStackTrace();
      }
  }
  
  public void createAndShowGUI() {
  
      JFrame frame = new JFrame("Error-Highlighter");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  
      JTextField field = new JTextField();
      field.setBounds(60,  100,  60,  80);
      field.setSize(new Dimension(500,200));
      field.setBackground(Color.LIGHT_GRAY);
      field.setVisible(true);                 
  
  
      field.setEditable(false);
  
  
      frame.add(field);
  
      JTextArea area = new JTextArea(30, 50);
      area.setLineWrap(true);
      area.setWrapStyleWord(true);
      area.setEditable(false);
  
  
      for(i = 0; i &lt; Retext1.length(); i++) {         
  
          char splitArray1 [] = Retext1.toCharArray();  
  
          s1 = s1 + splitArray1[i];           
      }
  
  
      System.out.println(s1.charAt(s1.length() - 1));
      area.setText(s1);
  
  
      for(j = 0; j &lt; Retext2.length(); j++) {         
  
          char splitArray2 [] = Retext2.toCharArray();  
  
          s2 = s2 + splitArray2[j];           
      }
  
      System.out.println(s2.charAt(s2.length() - 1));
      field.setText(s2);
  
  
  
  
      String SplitArray1[] = Retext1.split(" ");
      String SplitArray2[] = Retext2.split(" ");
  
      //System.out.println(SplitArray1.length);
  
    if(SplitArray1.length &gt; SplitArray2.length)  
        length = SplitArray1.length;
    else if(SplitArray2.length &gt; SplitArray1.length)
        length = SplitArray2.length;
    else length = SplitArray2.length;
  
  
            for(x = 0; x &lt; length; x++) { 
  
          if(SplitArray1[x] == SplitArray2[x]) 
              continue;
  
          else if(!SplitArray1[x] .equals(SplitArray2[x])) {
  
  
  
                  token1 = "" + SplitArray1[x];
                  token2 = "" + SplitArray2[x];
  
  
                  System.out.print(x);
                  System.out.print("  :  ");
                  System.out.print(token1);
                  System.out.print("  :  ");
                  System.out.print(token2);
                  System.out.println();
  
                  highlight(area, token1);                             
                  highlight(field, token2);                       
  
              }   
  
         }      
  
  
      frame.getContentPane().add(new JScrollPane(area), BorderLayout.CENTER);               
      frame.pack();
      frame.setVisible(true);
      frame.setLocationRelativeTo(null);
  }
  
  
  public void highlight(JTextComponent textComp, String pattern) {
  
      removeHighlights(textComp);
  
      try {
  
          Highlighter hilite = textComp.getHighlighter();
          Document doc = textComp.getDocument();
          String text = doc.getText(0, doc.getLength());
  
          int position = 0;
  
          while ((position = text.indexOf(pattern, position)) &gt;= 0) {
  
              hilite.addHighlight(position, position + pattern.length(), myHighlightPainter);
              position += pattern.length();
          }
  
      } catch (BadLocationException e) {
          e.printStackTrace();
      }
  }
  
  
  public void removeHighlights(JTextComponent textComp) {
  
  
      Highlighter hilite = textComp.getHighlighter();
      Highlighter.Highlight[] hilites = hilite.getHighlights();
  
       for (int i = 0; i &lt; hilites.length; i++) {
  
           if (hilites[i].getPainter() instanceof MyHighlightPainter) {
              hilite.removeHighlight(hilites[i]);
          }
      }
  }
  
  Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(Color.red);
  
  
  class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter {
  
      public MyHighlightPainter(Color color) {
  
          super(color);
  
      }
    }
  }
  </code></pre>