-1

String stored contains "1.0" in it. and I want to increase its value by 0.5, every time I press the button. But instead, my output becomes "1.00.5". How do I fix this?

 String stored = userspeed.getText().toString();
 String speedplus = stored + 0.5;
 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
 SharedPreferences.Editor editor = preferences.edit();
 editor.putString("user_speed", speedplus.toString());
 editor.apply();

UPDATE

public class ProgramActivity extends AppCompatActivity {
    EditText userspeed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_program);
        userspeed = (EditText) findViewById(R.id.userspeed);

        //load values
        SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(getBaseContext());
        String stored = preferences.getString("user_speed", "1.0");//default
        userspeed.setText(stored, TextView.BufferType.EDITABLE);

    }

    public void adduserspeed(View view) {
        String stored = userspeed.getText().toString();
        double storedValue = Double.parseDouble(stored);
        String speedplus = String.valueOf(storedValue +0.5f);
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("user_speed", speedplus);
        editor.apply();
    }
}
  • 2
    It is not an `int`. It is `String` so it will append. Use `int` – Piyush May 09 '17 at 07:15
  • Parse stored (i.e. 1.0 as float and add 0.5f) then convert it to String againt – PEHLAJ May 09 '17 at 07:22
  • Yes, you can use double as well like Double.parseDouble(stored) + 0.5d, please see my answer below – PEHLAJ May 09 '17 at 07:28
  • @user12345 just wondering, why would you add an "f" to it? – user7984644 May 09 '17 at 07:41
  • http://stackoverflow.com/questions/9748160/why-f-is-placed-after-float-values – PEHLAJ May 09 '17 at 07:44
  • http://stackoverflow.com/questions/14102955/java-why-do-you-need-to-specify-an-f-in-a-float-literal – PEHLAJ May 09 '17 at 07:44
  • Because otherwise it defaults to double, which is a more commonly used floating point type than float. f means we are trying add float value, d for double. It is best practice to use such convention. – PEHLAJ May 09 '17 at 07:45

5 Answers5

0
    String stored = userspeed.getText().toString();
    float speedplus = Float.parseFloat(stored) + 0.5;
    SharedPreferences preferences = PreferenceManager
            .getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putFloat("user_speed", speedplus);
    editor.apply();

Parse the string value to int and then do mathematical operation. Otherwise string concatenation will happen.

VishnuSP
  • 599
  • 5
  • 16
0

Try parsing stored i.e. 1.0 as Double and then add 0.5d

String speedplus = String.valueOf((Double.parseDouble(stored) + 0.5d));

Final code

String stored = userspeed.getText().toString();
String speedplus = String.valueOf((Double.parseDouble(stored) + 0.5d));
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("user_speed", speedplus.toString());
editor.apply();
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
0

The quickest hack would be:

String speedplus = String.valueOf(Float.parseFloat(stored) + 0.5F);

However, your next question will no doubt be "Why is my result 1.499999999?", which is answered here in detail.

Community
  • 1
  • 1
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
0

You can also use double like:

 String speedplus = (Double.parseDouble(stored)+0.5).toString();
Kaushal28
  • 5,377
  • 5
  • 41
  • 72
0

First, you need to convert your string value to a float value, then perfom the add operation and finally convert the result to a string so you can store as a string in shared preferences.

    final String stored = userspeed.getText().toString();
    final float storedValue = Float.parseFloat(stored);
    final String speedplus = String.valueOf(storedValue + 0.5f);
    final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    final SharedPreferences.Editor editor = preferences.edit();
    editor.putString("user_speed", speedplus);
    editor.apply();

If you want to use a double value, simply replace first lines as the follows:

final String stored = "1.0";
final double storedValue = Double.parseDouble(stored);
final String speedplus = String.valueOf(storedValue + 0.5d);

UPDATE Your problem is that within adduserspeed(View view) method your are not retrieving the stored value from preferences, your are retrieving from the EditText, and you are not updating that EditText value, so every time you execute adduserspeed(View view) method, the value you are storing on preferences is 1.0 + 0.5, because you EditText value is 1.0 until you re-open your app. When you re-open your app your EditText value is 1.5 and so on...

I have improved your code and now works well, anyway, do not just copy my code, try to understand it so you can learn.

public class MainActivity extends AppCompatActivity {
private EditText userSpeed;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.userSpeed = (EditText) findViewById(R.id.userspeed);
    setUserSpeedText();

}

public void adduserspeed(View view) {
    final String stored = getUserSpeedValue();
    final double storedValue = Double.parseDouble(stored);
    final String speedPlus = String.valueOf(storedValue + 0.5f);
    setUserSpeedValue(speedPlus);
    setUserSpeedText();
}

private String getUserSpeedValue() {
    final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    return preferences.getString("user_speed", "1.0");
}

private void setUserSpeedValue(final String newSpeedValue) {
    final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    final SharedPreferences.Editor editor = preferences.edit();
    editor.putString("user_speed", newSpeedValue);
    editor.apply();
}

private void setUserSpeedText() {
    if (null != this.userSpeed) {
        this.userSpeed.setText(getUserSpeedValue());
    }
}
}
AlexTa
  • 5,133
  • 3
  • 29
  • 46