0

I'm trying to find the sum of some elements of a Java array. This array has 5 elements and if three of its elements have the same value I want the sum to be

sum += numrat[i];

Firstly I order them in ascending order. Then put the if condition. When I try to execute this an error occurs in the line of if condition.

Can someone please show me how to make this control of the elements if three of them have the same value? This is my code:

public int katerNjesoj()
{
     int[] numrat=new int[zar.length];
     for(int i=0;i<zar.length;i++)
         numrat[i]=zar[i].getValue();
     int sum=0;
     for(int i=0;i<zar.length;i++)
     {
         if(numrat[1]==numrat[2]==numrat[3]|| numrat[2]==numrat[3]==numrat[4]||numrat[3]==numrat[4]==numrat[5]  )

             sum+=numrat[i];

     }
     return sum;
}
1
  • you shouldn't put that if in the for loop, you should put the for loop in the if statement, and split it in more conditions using the AND ( && ) operator Commented Jan 26, 2016 at 11:33

2 Answers 2

1

You can't compare three numbers with numrat[1]==numrat[2]==numrat[3], since the first comparison returns a boolean value, which can't be compared to the 3rd number.

Change it to

(numrat[1]==numrat[2] && numrat[2]==numrat[3])

Beside that, you want to calculate the sum only if your condition is met, so your loop should be inside the if statement and not the other way around :

if ((numrat[0]==numrat[1] && numrat[1]==numrat[2])||(numrat[1]==numrat[2]&&numrat[2]==numrat[3])||(numrat[2]==numrat[3]&&numrat[3]==numrat[4]))
    for(int i=0;i<numrat.length;i++) {
        sum+=numrat[i];
    }

Note that the array indices should be between 0 and 4, not between 1 and 5.

In addition, you can make the if condition more general by making a for loop that would compare 3 (or any number of) consecutive elements of the array in each iteration and ends when a match is found.

For example :

boolean found = false;
for (int i = 0; i < numrat.length - 2; i++) {
    if (numrat[i] == numrat[i+1] && numrat[i+1] == numrat[i+2]) {
        found = true;
        break;
    }
}
if (found) {
    for(int i=0;i<numrat.length;i++) {
        sum+=numrat[i];
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

ok thanks but how can I control if 3 of them have the same value but using numrat[i] and not numrat[1],numrat[2] etc?
1

You can't compare 'tripled' condition, you need to split it to two chcks

if((numrat[1] == numrat[2] && numrat[2] == numrat[3]) || (numrat[2] == numrat[3] && numrat[3] == numrat[4]) || (numrat[3] == numrat[4] && numrat[4] == numrat[5]))

As a side note, Array indexes are from 0 to array size -1, not 1 to array size. So you actually need

if((numrat[0] == numrat[1] && numrat[1] == numrat[2]) || (numrat[1] == numrat[2] && numrat[2] == numrat[3]) || (numrat[2] == numrat[3] && numrat[3] == numrat[4]))

In addition, its not necessary to do check the conditions each iteration, it either true or false for the numbers in the current array. Just put the if outside the loop

if((numrat[0] == numrat[1] && numrat[1] == numrat[2]) || (numrat[1] == numrat[2] && numrat[2] == numrat[3]) || (numrat[2] == numrat[3] && numrat[3] == numrat[4])) {
    for(int i = 0 ; i < zar.length ; i++) {
        sum+=numrat[i];
    }
}

Edit

To check if there are 3 equal values you can use HashMap

Map<Integer, Integer> numCount = new HashMap<>();
boolean hasThree = false;

for(int i = 0 ; i < zar.length && !hasThree; i++) {
    if (numCount.containsKey(numrat[i])) {
        numCount.put(numrat[i], numCount.get(numrat[i]) + 1);
        if (numCount.get(numrat[i]) == 3) {
            hasThree = true;
        }
    }
    else {
        numCount.put(numrat[i], 1);
    }
}

If the map has the number we increment the value by one, and if it doesn't we add the number to the map with value one. If hasThree is true we found three equal values.

You can of course combine the counting with the check, if hasThree is true return the sum otherwise return 0 (by your logic);

1 Comment

ok thanks but how can I control if 3 of them have the same value but using numrat[i] and not numrat[1],numrat[2] etc?

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.