0

I am new to android studio and java is pretty new to me. I am trying to make a conversion app. For that I have to get the value of a edit text box and turn it into a number. That's when my app crashes, the moment I add :

double n1Var = Double.parseDouble(n1.getText().toString());

My app opens and then immediately closes. If I delete it the app works fine. I tried different way to turn it into a double, tried even turning it into a integer still does not work. If I try a string it works alright.

Ahmad
  • 69,608
  • 17
  • 111
  • 137

4 Answers4

1
//assuming that (EditText)n1 = (EditText)findViewById(R.id.id_of_n1);

String et_val = n1.getText().toString().trim();
double n1Var;

if(et_val.equals("") || et_val.length==0 ){

/*you can improve on this filter with a regex to check if the content of the edit text is event a number. 

Furthermore, you can also control the entry of the information allowed into edit text with a switch in the xml for the edit text by allowing for only entry of numbers.

You can also do control the entry into the edit text with a java code that would compel the user to only enter numbers if that complies with what you are looking forward to executing with the user.

*/ 
    n1Var = 0;

}else{
    n1Var = Double.parseDouble(et_val);
}

//Hope this helps. Cheers and happy coding.

AppEmmanuel
  • 144
  • 1
  • 6
0

From Java Documentation, here are Exceptions that can be thrown from Double.parseDouble:

Throws:

NullPointerException - if the string is null

NumberFormatException - if the string does not contain a parsable double.

From your description, it's probable that NullPointerException was thrown. Check the logcat to find out exactly which exception was thrown.

Community
  • 1
  • 1
Trent Oh
  • 179
  • 2
  • 6
0

nothing seems to work .Here is my code + the log for when I press the button to open the activity where the double is ...

Log screenshot

EditText ETInput;
Button BConvert;
TextView TVresult;
RadioButton c2f;
RadioButton c2k;
RadioButton f2c;
RadioButton f2k;
RadioButton k2c;
RadioButton k2f;

@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_converter);


   ETInput = (EditText)findViewById(R.id.ETInput);
    double a = Double.parseDouble(ETInput.getText().toString());
   BConvert = (Button)findViewById(R.id.BConvert);
   TVresult = (TextView)findViewById(R.id.TVresult);
   c2f = (RadioButton)findViewById(R.id.c2f);
   c2k = (RadioButton)findViewById(R.id.c2k);
   f2c = (RadioButton)findViewById(R.id.f2c);
    f2k = (RadioButton)findViewById(R.id.f2k);
    k2c = (RadioButton)findViewById(R.id.k2c);
    k2f = (RadioButton)findViewById(R.id.k2f);




    String val = ETInput.getText().toString().trim();
    double var;
    var = Double.parseDouble(val);




}

}

0

with this:

double w;

    try {
        w = new Double(input.getText().toString());
    } catch (NumberFormatException e) {
        w = 0;
    }

w is always a zero