Search code examples
javareturn

Why does my method return the wrong value?


Even though my method operationsNeeded prints the correct value for my return-int "count1", the very next line it returns something else to my main method. I did not include the rest of my code, if needed I'd gladly provide it. For example if operationsNeeded is executed 4 times, count1 is on 4 which is printed out as well. But for reasons unknown to me the System.out.println("check: " +count1); Statement is executed 4 times like this:
check: 4
check: 4
check: 3
check: 2

I would expect my program to execute this only once and then continue to the return statement.

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    int testcases = sc.nextInt();
    int count =0;
    while (count<testcases){
        int numberOfColleagues = sc.nextInt();
        sc.nextLine();
        String startPieces = sc.nextLine();

        int[] listOfcolleagues = listOfColleagues(numberOfColleagues, startPieces);

        int count2 = operationsNeeded(listOfcolleagues, 1);
        count++;
        System.out.println(count2);
    }

}

 public static int operationsNeeded (int[] listOfColleagues, int count1){
    //remove duplicates first
    ArrayList<Integer> relevantList=removeDuplicatesAndSort(listOfColleagues);
    System.out.println("relevantlist" + relevantList);
    //check for smallestdelta & index
    int [] deltaAndIndex = smallestDeltaHigherIndex(relevantList);
    int delta = deltaAndIndex[0];
    int index = deltaAndIndex[1];

    if (delta==1){
        for (int i=0;i<relevantList.size();i++){
            if (i!=index){
                relevantList.set(i,relevantList.get(i)+1);
            }

        }
    }
    if (delta>1 && delta<5){
        for (int i=0;i<relevantList.size();i++){
            if (i!=index){
                relevantList.set(i,relevantList.get(i)+2);
            }
        }
    }
    if (delta>4){
        for (int i=0;i<relevantList.size();i++){
            if (i!=index){
                relevantList.set(i,relevantList.get(i)+5);
            }
        }
    }

    System.out.println(count1);
    int[] updatedList = new int[relevantList.size()];
    for (int i=0; i<relevantList.size();i++){
        updatedList[i]=relevantList.get(i);
    }
    if (!isAllTheSame(relevantList)) {
        count1 +=1;
        operationsNeeded(updatedList,count1);
    }

        System.out.println("check: " + count1);

 return count1;
}

Solution

  • Your method is recursive. The "check: " line is printed on each level of that recursion, with the value that it currently has on that level. It first prints the "inner-most" value (4), than that of the level above (also 4), and finally hte value in the top-level, which is 2 after being incremented in the if above. And the value it returns is always the value from to top-level.

    If you want to print it only once, you could print it on the inner-most level only, using else. However, that will still return the value from the top-level iteration; instead, keep track of the value returned from the recirsive call and update count1 accordingly.

    if (! isAllTheSame(relevantList)) {
        // we have to go deeper!
        count1 = operationsNeeded(updatedList, count1 + 1);
    } else {
        // phew, finally done
        System.out.println("check: " + count1);
    }