Nauman Aslam
Nauman Aslam

Reputation: 299

Getting value in String

Im using this code to get value from a string and then posting it after performing operation.

The problem is that after entering some value when I try to erase the entire text field it stops unexpectedly.Im using onTextChanged method and the piece of code that Im working with is here. Also it works fine when I use a Button to submit instead on textWatcher.

min=(EditText) findViewById(R.id.minutes);
sec=(EditText) findViewById(R.id.seconds);
millisec=(EditText) findViewById(R.id.milliseconds);
String minute = min.getText().toString();
Double   min1=Double.parseDouble(minute);
            min1=min1*60;
Double  min2=min1*1000;
sec.setText(Double.toString(min1));
millisec.setText(Double.toString(min2));

Upvotes: 0

Views: 66

Answers (1)

Fortega
Fortega

Reputation: 19682

parseDouble will throw a NumberFormatException if you do not pass a parsable String... So if you empty the field, you will get an exception.

You can wrap your code in a try-catch-clause:

String minute = min.getText().toString();

try{
   Double min1 = Double.parseDouble(
   min1=min1*60;

   Double min2=min1*1000;
   sec.setText(Double.toString(min1));
   millisec.setText(Double.toString(min2));
} catch(NumberFormatException e){
   sec.setText("");
   millisec.setText("");
} 

Upvotes: 2

Related Questions