Breanna Jury
Breanna Jury

Reputation: 29

Convert String input to Double and avoid java.lang.NumberFormatException

I need to convert a String input to a double so that I can create a toast pop-up if the input is out of a certain range (using if statements with < and > operators). Edit: input1 is a user input saved in room

I've tried converting the String to a double using this code Double d = Double.parseDouble(String.valueOf(input1)); but this comes up with the error "Caused by: java.lang.NumberFormatException: For input string: "com.example.mysugartracker.Input1@d89771f"" Within this class, I retrieve input1 and use the inputs to fill a recyclerView, which leads me to believe that retrieving input1 is not the problem, but instead the method of converting it to a double. (Though I am only a beginner at using android studio so I could be wrong)

Tabbed.java (class where the pop-up is displayed)

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mInputViewModel = ViewModelProviders.of(this).get(InputViewModel.class);
        if (requestCode == INPUT_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
            Input1 input1 = new Input1(data.getStringExtra(input.EXTRA_REPLY));
            mInputViewModel.insert(input1);
            ** Double d = Double.parseDouble(String.valueOf(input1)); **
            //display pop-up if blood is above target
            if (d > 8.0) {
                Toast.makeText(
                        getApplicationContext(),
                        R.string.PopupHigh,
                        Toast.LENGTH_LONG).show();
            }
            //display pop-up if blood is below target
            if (d < 4.0) {
                Toast.makeText(
                        getApplicationContext(),
                        R.string.PopupLow,
                        Toast.LENGTH_LONG).show();
            }
            } else {
                Toast.makeText(
                        getApplicationContext(),
                        R.string.empty_not_saved,
                        Toast.LENGTH_LONG).show();
                }
        }
    public static final int INPUT_ACTIVITY_REQUEST_CODE = 1;

input1.java (entity class where the user input is saved)

public class Input1 {
    @PrimaryKey(autoGenerate = true)
    private int id;
    @NonNull
    @ColumnInfo(name = "input1")
    private String mInput1;

    Input1(@NotNull String input1) {this.mInput1 = input1;}
    public String getInput1() {return this.mInput1;}

    //getter for id
    public int getId() {
        return id;
    }
    //setter for id
    public void setId(int id) {
        this.id = id;
    }

}

I've marked the problematic code with **

I need a different way to convert the String input to a double without causing a java.lang.NumberFormatException or a way to avoid this error.

Upvotes: 0

Views: 1192

Answers (1)

Captain
Captain

Reputation: 744

I don't know which methods are available on Input1, but your problem will be solved by simply calling the method that gives you the value. String.valueOf(input1) returns the String representation of the object, which means it simply calls the toString() method, and in your case Input1 does not override toString therefore it is Object.toString() that is being called.

Change it to something like this:

Double d = Double.parseDouble(input1.getValue());

Or, alternatively, implement Input1.toString():

UPDATED with solution based on comments:

public class Input1 {
    @PrimaryKey(autoGenerate = true)
    private int id;
    @NonNull
    @ColumnInfo(name = "input1")
    private String mInput1;

    Input1(@NotNull String input1) {this.mInput1 = input1;}
    public String getInput1() {return this.mInput1;}

    //getter for id
    public int getId() {
        return id;
    }

    //setter for id
    public void setId(int id) {
        this.id = id;
    }

    // Just add this method to you Input1 class
    @Override
    public String toString() {

        return mInput1;
    }
}

Upvotes: 1

Related Questions