Question: I am stuck here:

System.out.println("Enter the number to be added to these factorial numbers:");
 System.out.println("1!/1 + 2!/2 + 3!/3 + "); //factorial ie. 5!/5 = 4 \* 3 \* 2 \* 1 
 n = scan.nextInt();

```
     while (!exit)
    {

        if (n <= 2) 
        {
            sum = 1;
            exit = true;
        }
        sum = sum + i - 1;

        i = i - 1;
        if (i == 2)
        {
            exit = true;
        }
    }

     //m2 = 1 * 1 + (2 * 2) + (2 * 1) + (3 * 3) + (3 * 2) + (3 * 1) + (n * n -1);  
     //System.out.println(m2);

```

Answer: 
A simple and elegant solution is to use a very simple library TinyDB in android, which is nothing but an abstraction over the Shared Preferences.

You can initialise it in your activity's `onCreate()` method like this:

```
TinyDB tinydb = new TinyDB(this);

```

In fragment, just replace `this` with `getActivity`.

And then use it like this:

```
tinydb.putString("userName", "john");
String currentUser = tinydb.getString("userName");

```

Hope it helps.
You should store data in SharedPreferences rather than global variables because when app crashes, the data updated in static global variables at different times is lost and the default data in it remains.