-4

I'm trying to take an int from SharedPreferences that tracks how long the app program has been active and reduce it to an int less than 7 to show what day of that program week the user is in.

Here is what I have used to reduce the int, (i am trying to use a while loop to go in and subtract 7 until the int is less than 7. The IF statement was added to see if the int was reduced and it was displaying the wrong String)

public void methodNameHere() {
        SharedPreferences onvalue = getApplicationContext().getSharedPreferences("TRAININGDAY", MODE_APPEND);
        int day = (int) onvalue.getLong("DAY", 0);
        SharedPreferences.Editor edit = onvalue.edit();

        while (day < 7) {
            day = day - 7;
        }

        if (day < 7) {
            String da = Integer.toString(day);
            Toast.makeText(CardioStart.this, da, Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(CardioStart.this, "f?*k", Toast.LENGTH_LONG).show();
        }
    }

And this is where the int is coming from:

private void dayOfTraining() {

        TextView day = (TextView) findViewById(R.id.dayoftraining);
        date.setTimeInMillis(millidate);

        Calendar someday = Calendar.getInstance();
        someday.set(2015, 4, 25);

        Calendar today = Calendar.getInstance();
        Long millidate = someday.getTimeInMillis();
        long timeTwo = today.getTimeInMillis();
        long oneDay = 1000 * 60 * 60 * 24;
        long delta = (timeTwo - millidate) / oneDay;
        long actualday = delta + 1;
        day.setText(Long.toString(actualday));

        SharedPreferences onvalue = getApplicationContext().getSharedPreferences("TRAININGDAY", MODE_APPEND);
        SharedPreferences.Editor edit = onvalue.edit();
        edit.putLong("DAY", actualday);
        edit.commit();

    }

String da toasts as "29" so it seems its not reducing.

I feel like I am missing something simple or I just don't have a true grasp on the use of while loops. Any help would be greatly appreciated. Thank you.

Ankit
  • 429
  • 3
  • 16
Mike Maye
  • 13
  • 1
  • 4

1 Answers1

1

The while statement is wrong, you should be checking while(day > 7) -> do this not while(day < 7) . Code -

while (day > 7)
{
day = day - 7;
}
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • Wow. Just wow. I'm a dummy. Thanks for pointing that out, and not blowing up my spot for being a dunce. Cheers! – Mike Maye Jun 22 '15 at 17:59