1

I am trying to compare two arrays and display which entries are present, and which are not.

For example sake - my two arrays contain the following:

logArray: Test1, Test2, Test3, Test4, Test5
checkArray: Test1, Test2, Test3, Test4, Test5, Test6, Test7

The result I am trying to achieve is the following output:

FOUND: Test1
FOUND: Test2
FOUND: Test3
FOUND: Test4
FOUND: Test5
NOT FOUND: Test6
NOT FOUND: Test7

I think I am close with my code but something is just not right and the NOT FOUND displays a ridiculous amount.

    for (int i = 0; i < logArray.length; i++) {
        for (int x = 0; x < checkArray.length; x++) {
            if (logArray[i].equals(checkArray[x])) {
                logsFound = logsFound + 1;
                modelFound.addElement("<html><font color=#009933>FOUND: </font> " + logArray[x] + "</html>");
            }
        }
        if (isFound == false) {
            logsNotFound = logsNotFound + 1;
            modelNotFound.addElement("<html><font color=#FF0000>NOT FOUND: </font> " + logArray[i] + "</html>");
        }         
    }

EDIT: Adjusted as per some comments, and it displays the NOT FOUND correctly now, HOWEVER it'll only display the amount of NOT FOUND if it's within the first array amount. So because my logsArray contains 9 items, but checkArray contains 10 items (as seen under Checklist), it displays 4 total in NOT FOUND. So it's meant to display 2VA.004.01.16 too.

isFound is declared before the for loops

Picture

Updated code:

    for (int i = 0; i < logArray.length; i++) {
        isFound = false;
        for (int x = 0; x < checkArray.length; x++) {
            if (logArray[i].equals(checkArray[x])) {
                logsFound = logsFound + 1;
                modelFound.addElement("<html><font color=#009933>FOUND: </font> " + logArray[x] + "</html>");
                isFound = true;
            }
        }
        if (isFound == false) {
            logsNotFound = logsNotFound + 1;
            modelNotFound.addElement("<html><font color=#FF0000>NOT FOUND: </font> " + checkArray[i] + "</html>");
        }
    }
1
  • Where is isFound assigned? Commented Dec 15, 2015 at 15:48

7 Answers 7

3

You could build up a hash set once, to speed up the search.

Set<String> set = new HashSet<String>(logArray);
for(String item : checkArray)
{
    if (set.contains(item)){
        //emit found
    } else {
        //emit not found
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You should really set isFound to true when a value is found in the logsArray

for (int i = 0; i < logArray.length; i++) {
    for (int x = 0; x < checkArray.length; x++) {
            if (logArray[i].equals(checkArray[x])) {
                isFound = true; // <-Add this, because right now logsNotFound is incremented no matter what without this statement
                logsFound = logsFound + 1;
                modelFound.addElement("<html><font color=#009933>FOUND: </font> " + logArray[x] + "</html>");
            }
    }
    if (isFound == false) {
                logsNotFound = logsNotFound + 1;
                modelNotFound.addElement("<html><font color=#FF0000>NOT FOUND: </font> " + logArray[i] + "</html>");


    }
    isFound = false;
}

I'm assuming that isFound is declared somewhere outside the loops, but it's not changing in the loops. Therefore, after each iteration of the inner for loop, isFound stays constant (false) and (logsNotFound = logsNotFound + 1) is executed x times. And then after the if statement, reset isFound to false in preparation for the next iteration of the inner for loop.

EDIT: To address OP's edits to his/her question, the reason why there's only 4 strings not found is because the outer loop is only iterated logArray.length times, not checkArray.length times. Therefore, in this case for example, the outer loop will only run 9 times, not 10 times, never reaching checkArray[9] aka "2VA.004.01.16". So what I would do in this case is to switch the order of the for loops. Also, I would change the code so that you iterate through every element of the logArray and compare it to an element of the checkArray one at a time to see if it's there. So that when you find an element in checkArray in logArray, you add the element from checkArray, not logArray because in the end, you're trying to print out all the elements of checkArray. The way you had it before only prints out all the elements of logArray.

for (int i = 0; i < checkArray.length; i++) {
        for (int x = 0; x < logArray.length; x++) {
        isFound = false;
            if (logArray[x].equals(checkArray[i])) {
                isFound = true;
                logsFound = logsFound + 1;
                modelFound.addElement("<html><font color=#009933>FOUND: </font> " + checkArray[i] + "</html>");
            }
        }
        if (isFound == false) {
            logsNotFound = logsNotFound + 1;
            modelNotFound.addElement("<html><font color=#FF0000>NOT FOUND: </font> " + checkArray[i] + "</html>");
        }         
    }

3 Comments

Thank you for your help, we're getting closer! It's not showing all 10 items in the NOT FOUND section. i.imgur.com/sFEwMcs.png
FIXED IT! Had to add isFound = true; after it found the item. THANK YOU!
Ah yes, let me fix my answer to accommodate for these changes that I forgot to include
1

You don't seem to assign any value to isFound in your loop.

Comments

1

I believe you should assume isFound is false before checking all the items in checkArray and then if you find the item you should set isFound to true.

for (int i = 0; i < logArray.length; i++) {
    isFound = false;
    for (int x = 0; x < checkArray.length; x++) {
        if (logArray[i].equals(checkArray[x])) {
            logsFound = logsFound + 1;
            modelFound.addElement("<html><font color=#009933>FOUND: </font> " + logArray[x] + "</html>");
            isFound = true;
        }
    }
    if (isFound == false) {
        logsNotFound = logsNotFound + 1;
        modelNotFound.addElement("<html><font color=#FF0000>NOT FOUND: </font> " + logArray[i] + "</html>");
    }         
}

Comments

1

The isFound is always false since it is never modified

You can also check if item exists in logArray while iterating over checkArray

List<String> logList = Arrays.asList(logArray);

for(String item : checkArray)
{
    if( !logList.contains(item ) ){
        System.out.print( "NOT" );
    }
    System.out.print( "FOUND: " + item + "\n" );
}

Comments

1

If you want to use collections, here is a simple solution

String[] one = {"Test1", "Test2"};
String[] two= {"Test1", "Test2", "Test3", "Test4", "Test5"};
ArrayList<String> missingCodes = new ArrayList<>(Arrays.asList(two));
missingCodes.removeAll(Arrays.asList(one));
System.out.println(missingCodes);

output is

[Test3, Test4, Test5]

Comments

0

I prefer the solution by weston but if you're trying to work with arrays, there were a couple things to modify here to make it work as you intended.

It doesn’t appear that your isFound boolean is ever set to true in the loop, so there’s no way for NOT FOUND to not be displayed. Also, isFound needs to be initialized to false at the beginning of each loop. Once the value is found, you want to break out of the loop for better performance so throw a break in after the found.

Thoughts: Try not to use indexes like i and x. You’ll notice in your original code, the X index was being applied to the i array, which would result in an ArrayIndexOutOfBoundsException if the array sizes are not conducive. By design, the longer array has to be the logArray, which is also a pitfall in case the array sizes change.

for (int logIndex = 0; logIndex < logArray.length; logIndex++) {
        boolean isFound = false;
        for (int checkIndex = 0; checkIndex < checkArray.length; checkIndex++) {
            if (logArray[logIndex].equals(checkArray[checkIndex])) {
                logsFound = logsFound + 1;
                modelFound.addElement("<html><font color=#009933>FOUND: </font> " + logArray[logIndex] + "</html>");
                isFound = true;
                break;
            }
        }
        if (isFound == false) {
            logsNotFound = logsNotFound + 1;
            modelNotFound.addElement("<html><font color=#FF0000>NOT FOUND: </font> " + logArray[logIndex] + "</html>");
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.