Java, Spring : Text comparison sometimes adding null entries.

I am working on a Spring-MVC application using Hibernate as ORM and Postgres DB in which I have note taking application. Whenever an user edits a note, I am comparing the old entry in DB and the new text given by user. I am separating the text by dot operator and comparing them. The differences I am putting in 2 columns, namely oldText and newText.

The problem is sometimes I am getting 'null' entry prepended to the text. I have tried some ways to mitigate this issue, alas no success. Can someone help me with this. Thanks a lot.

Code :

List<String> oldTextList = Arrays.asList(mnotes1.getMnotetext().split("(\\.|\\n)"));
List<String> newTextList = Arrays.asList(mnotes.getMnotetext().split("(\\.|\\n)"));
int counter = Math.max(oldTextList.size(), newTextList.size());
String oldString = null;
String newString = null;

for (int current = 0; current < counter; current++) {

    if (oldTextList.size() <= current) {
        oldString = " ";
        newString = newTextList.get(current);

    } else if (newTextList.size() <= current) {
        oldString = oldTextList.get(current);
        newString = " ";
    } else {
        if (isLineDifferent(oldTextList.get(current), newTextList.get(current))) {
            noEdit = true;
            oldString += oldTextList.get(current);
            newString += newTextList.get(current);
        }
    }

    if(oldString != null && newString != null) {
        if(!(groupNoteHistory.getNewNoteText() == null)){
            if(!(groupNoteHistory.getNewNoteText().isEmpty())) {
                groupNoteHistory.setNewNoteText(groupNoteHistory.getNewNoteText() + "."+newString);
            }else {
                groupNoteHistory.setNewNoteText(newString);
            }
        }else {
            groupNoteHistory.setNewNoteText(newString);
        }
        if(!(groupNoteHistory.getOldText() == null)) {
            if(!(groupNoteHistory.getOldText().isEmpty())) {
                groupNoteHistory.setOldText(groupNoteHistory.getOldText()+"." + oldString);
            }else {
                groupNoteHistory.setOldText(oldString);
            }
        }else {
            groupNoteHistory.setOldText(oldString);
        }
    }
}

Database screenshots :

Old NoteText :

Thanks a lot... :-)

Answers


Change

String oldString = null;
String newString = null;

To

String oldString = "";
String newString = "";

Need Your Help

Add layers to a PSD file using ImageMagick

imagemagick psd

I have a lot of JPEG files and I want to add them as layers to a PSD file using ImageMagick

DotNetOpenAuth RelayParty not working on load balanced cluster

openid load-balancing dotnetopenauth cluster-computing web-garden

We're trying to move an ASP.NET MVC application, which uses DotNetOpenAuth OpenID Version 3.4.1, from a single server web garden to a physical server cluster held behind a hardware load balancer.