0

How can i make the overHalfSum integer add up with every for loop ? Note that the compiler warns me that overHalfSum is not used.

    int overHalfSum=0;

    for (int i=0;i<20;i++){
        if (sensorPol[i].getCo()>0.5){
            overHalfSum += 1;
        }
    }
    for (int i=0;i<20;i++){
        if (sensorTemp[i].getMax()>0.5){
            overHalfSum += 1;
        }
    }   
    for (int i=0;i<10;i++){
        if (camera[i].getLoad()>0.5){
            overHalfSum += 1;
        }
5
  • Are you sure you have made no typographical mistakes? At first glance this seems fine. Commented Apr 20, 2016 at 20:34
  • 5
    There's something else going on there - this looks fine. Is this the full code? The IDE might be warning you that it is never read but only written. Commented Apr 20, 2016 at 20:35
  • 1/ debug it, 2/ figure out that you never enter your ifs, 3/ profit Also note that the 2 first loops could be "collapsed" Commented Apr 20, 2016 at 20:35
  • 1
    You increment it but never read its value. Commented Apr 20, 2016 at 20:36
  • Thanks everyone ,it seems that the counter works just right ,but it is just the compiler that warns me cause i never read it .I just added a println and the error is gone .The counter works fine as well Commented Apr 20, 2016 at 20:54

2 Answers 2

1

You increment the variable, but never read its value. The code is equivalent to:

int overHalfSum=0;

for (int i=0;i<20;i++){
    sensorPol[i].getCo();
}
for (int i=0;i<20;i++){
    sensorTemp[i].getMax();
}   
for (int i=0;i<10;i++){
    camera[i].getLoad();
}

(and the calls in the for loops like sensorPol[i].getCo(); may be removable as well, if they have no side effect).

You need to actually read the variable's value for it to be "used", e.g. add this after the logic in your question:

System.out.println(overHalfSum);
Sign up to request clarification or add additional context in comments.

5 Comments

Not exactly, the variable is not read, but it might get incremented
If you never read it, it doesn't matter if you've incremented it.
yes, but the code is not "equivalent" because if it was, I could add anything under it without any trouble (IMHO, but it's rhetorical I agree)
Sure, you could add "anything" under it, and if that uses the variable, it's not unused.
Meh, we're pulling at straws here, have a +1.
0

The compiler says that you didn't use the variable because in the snipped you provided you only give the variable a value and change the value. You never use it in a function or with other variables.

Better check whether the conditions are met.

Maybe try to add a System.out.writeln('added 1 to overHalfSum') to see if it works like you want it to work.

if (sensorPol[i].getCo()>0.5){
    overHalfSum += 1;
    System.out.writeln('added 1 to overHalfSum');
}

3 Comments

Why do you think this changes whether overHalfSum is used? You're just printing an arbitrary string, and not using that variable.
He wonders why overHalfSum doesn't get incremented... and he only asked Why the compiler said that the overHalfSum variable isn't used, not how to use the variable
If you don't use the variable, you can't know (and it doesn't matter) if it is incremented.

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.