0

I am working on a Java webapp in which the user can make changes to a text area. In this, he can either write one paragraph, one sentence. So what I am currently trying to do is to split the whole paragraph by a dot separator. Once that is done, I would like to check which sentences have changed. I am currently doing it using for loop, which is not accurate as I have to length of array to Math.minimum of both String arrays. But it is not working, I am getting zero String modified from it. Kindly let me know what I am doing wrong.

Code :

 String oldText = "";
 String newText = "";

  if(!(oldNotes.getNotetext().equals(newNotes.getNotetext()))){
            String[] strings = newNotes.getNotetext().split(".");
            if(strings.length>0){
                String[] oldNotesArray = oldNotes.getNotetext().split(".");
                if(oldNotesArray.length>0){
                    for(int i=0; i<Math.min(strings.length,oldNotesArray.length);i++){
                        if(!(strings[i].contains(oldNotesArray[i]))){
                            oldText += oldNotesArray[i];
                            newText += strings[i];
                        }
                    }
               }
                noteHistory.setNewText(newText);
                noteHistory.setHistoryText(oldText);
            } else {

                noteHistory.setNewText(newNotes.getNotetext());
                noteHistory.setHistoryText(oldNotes.getNotetext());
            }
        }

So basically in both the Strings I would just like to save which sentences have been changed. That's all. Kindly let me know. Thanks. I am just using a dot separator for convenience, if there is any other advanced Regular Expression, I don't mind using it. Thanks a lot. :-)

Edit As per the suggestions, I am trying to use the google diff-match-patch library. This is what I was able to find, but still no success in isolating the differences. I have to persist them in database, so I will have to mark them as well, but I don't have them yet. My code :

 diff_match_patch diffMatchPatch = new diff_match_patch();
                LinkedList<diff_match_patch.Diff> deltas = diffMatchPatch.diff_main(oldText,newText);
                for(diff_match_patch.Diff d : deltas){
                    if(d.operation== diff_match_patch.Operation.DELETE)
                        oldText += d.text;
                    else if(d.operation== diff_match_patch.Operation.INSERT)
                        newText += d.text;
                    else
                    {
                        oldText += d.text;
                        newText += d.text;
                    }
                }

                System.out.println(oldText);
                System.out.println(newText);

Now when I print the text, I can see the whole 2 paragraphs. What am I doing wrong?

We are Borg
  • 5,117
  • 17
  • 102
  • 225

1 Answers1

0

use split("\\.") for getting exact array of string split-ted by String and than comapre using this link of google also have look on this artical

Community
  • 1
  • 1
Bhargav Modi
  • 2,605
  • 3
  • 29
  • 49
  • Thanks for the link. Do you have any examples how I can use it in this context. Thanks a lot. – We are Borg Mar 04 '15 at 14:20
  • @WeareBorg i dont have any examples but you can google it easily and you can get it. consider it a upvote or correct answer if it helped you some what!!! :) – Bhargav Modi Mar 05 '15 at 06:31