Emil Ivanov Emil Ivanov - 1 month ago 25
Java Question

int cannot be dereferenced java

Currently, a beginner in java. I am working on a code that will search for a value in an array and then return how many values it has found from that array.

public int getNumberOfReadings(int location)
{
int numberOfEntries=-1;
for(Reading readings : readings) {

if(readings.getLocation().contains(location)) {
numberOfEntries++;

}
}
return numberOfEntries;

}


It is telling me that int cannot be dereferenced which makes sense because int is primitive. Even though I know this, I still cannot get my head around coding in a way that will fix this error.

Answer

Your error is this part:

for (Reading readings : readings)

You kept calling both of them as readings; where you cannot do that and you need to keep separate names.

for (Reading reading : readings)

Try doing that instead. Regarding your error, I assume that because Reading.getLocation() is returning an int which is a primitive, and hence you cannot call contains() on it.

And for more good answers, we require you to give the correct error returned.