Click here to Skip to main content
15,905,323 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to compare two lists to one another. Loop through each index in both lists and compare the values to each other.
1. One position does not match: if loop1[i] != loop2[j] and i ==j, value at i in loop1 does not match value in loop2.
2. match but not at position: loop1[i]== loop2[j] and i!=j, value is common in both loops but is at wrong position.
3. if the first occurs more than once, multiple values dont match.

Problem is when i test it, i get the wrong output e.g loop 1 = 1,2,3,4,5,6,7 and loop2 = 1,2,3,5,5,7. It prints the first outcome. Correct
problem is here:
loop1 = 1, 2, 3, 4, 5, 6, 7
loop2 = 1, 2, 4, 3, 5, 6, 7, supposed to print second outcome but prints first outcome instead.
Eg
selectedindices = List.of(1, 2, 3, 4, 5, 6)


indicesofpositionguessed = List.of(1, 2, 3, 5, 5, 6


when you compare the two, its supposed to be: "User guess at position 3 is correct but at wrong position" but instead is "User guess at position 3 did not match selected"


Also for some reason the method returns a string but does not recognize the returned strings in the for loop but makes me put a return statement outside of the for loop. Why? Is there any way to get around it?

What I have tried:

Java
public String guessPosition(List<Integer> indicesOfPositionGuessed) {
       int count = 0;
       for (int i = 0; i < indicesOfPositionGuessed.size(); i++) {
           Integer item1 = indicesOfPositionGuessed.get(i);
           for (int j = 0; j < selectedIndicies.size(); j++) {
               Integer item2 = selectedIndicies.get(j);
               if (i != j) {
                   if (item1.equals(item2) == true) {
                       return "User guess at position " + i + " is correct but at wrong position";
                   }
               }
               if (i == j) {
                   if (item1.equals(item2) == false) {
                       count++;
                       return "User guess at position " + i + " did not match selected";
                   }
               }

           }
       }
       if(count > 0)
           return "User guess did not match in two positions";
       return null;
   }
Posted
Updated 10-Sep-18 0:44am
v2
Comments
Patrice T 9-Sep-18 16:24pm    
Add lists that give the wrong result.
Member 13978326 9-Sep-18 16:41pm    
i dont understand what you mean
Patrice T 9-Sep-18 16:45pm    
Show us the inputs that give wrong results.
Change your code so we can run it and get wrong result.
Member 13978326 9-Sep-18 16:52pm    
selectedindices =
List.of(1, 2, 3, 4, 5, 6)


indicesofpositionguessed =
List.of(1, 2, 3, 5, 5, 6


when you compare the two, its supposed to be: "User guess at position 3 is correct but at wrong position" but instead is "User guess at position 3 did not match selected"
Patrice T 9-Sep-18 17:04pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.

Quote:
when you compare the two, its supposed to be: "User guess at position 3 is correct but at wrong position" but instead is "User guess at position 3 did not match selected"

I don't see anything obvious in code to explain that answer, the way to know what is going on is to use the debugger and that the 2 lists are what you expect, then set a breakpoint and watch the code perform.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your cpde is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Well, it is simple, when i=3 and j=3 condition (1) is satisfied and the function returns.
Condition (2) would be satisfied when i=3 and j=4 but the function won't reach such a point.

That is, for each of the guessed indices you have to search in the selected ones array for a match, in order to check condition (2). On unsucessful search, condition (1) is automatically satisfied.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900