Jack
Jack

Reputation: 79

Count function not complete

I'm developing an app in which I need a counter. I have this counter within an if-clause and every time this block is triggered it should add 1 (one) to the current number. So if current number is: 1 and the if-block is triggered, the current number should be 2, then 3 for the next and so on... how ever. While testing this, the current number is changing to 2 but it wont count more. I'm sure I have written something wrong but can't seem to find what.

Code below:

@Override
public void onSensorChanged(SensorEvent event) {
    float presure = event.values[0];
    altitude = SensorManager.getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE, presure);
    System.out.println("altitude => " + altitude);
    Log.d("SENSOR alt: ", String.valueOf(altitude));
    int currentHigh = (int) altitude;
    int reps = 1;
    if (currentHigh > 44){
        reps = reps+1;
        mTextView.setText(String.valueOf(reps));
    }
    while(reps == 5){
        Vibrator vibrate = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        vibrate.vibrate(500);
        break;
    }
} 

Upvotes: 0

Views: 74

Answers (1)

davidxxx
davidxxx

Reputation: 131324

I suppose the reps variable is your counter as this code values always reps with the 2 value and this behavior matches with your remark :

While testing this the current number is changing too 2 but it wont count more. Im sure I have written something wrong but can't seem to find what.

Here you declare repsas a local variable and not as an instance field.
So at each call of the method, reps is overwritten :

int reps = 1;
if (currentHigh > 10) {
    reps = reps+1;
    mTextView.setText(String.valueOf(reps));
}

reps == 2 after each call by assuming currentHigh > 10.

Just declare reps as an instance field (outside the method) and it should solve your problem.

Upvotes: 1

Related Questions