0

I am a beginner to Android programming.

I am trying to convert an string(from an EditText widget) to a double

Whenever i run the program, it crashes whenever the parseDouble function is used:

Double.parseDouble( input.getText().toString() );

Does anybody have an idea why parseDouble causes my program to crash and a possible fix? or a different way to convert my input to a double?

2
Contributors
9
Replies
10
Views
6 Years
Discussion Span
Last Post by nerdy9000
0

Exact error that you get would be nice. Without it it is just guess work

My project compiles without error. But upon activation of the function, the android application crashes with the error: "Sorry! the application converter[app name] has stopped unexpectedly. Please try again."

0

I am currently working on a simple android application. But I have hit a snag.

I am trying to get a numerical value from an EditText field input and store it in a double named number.

I have the function activated by a button.
The project compiles and runs with no errors.

Upon clicking the button, the application crashes with the error "The Application converter has stopped unexpectedly. Please try again "

convertButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {
            
            	number = Double.valueOf( input.getText().toString() );  
            	output.setText(" " +number);

            }
        });
0

1. Your above post was moved from Java to Mobile Development, I do not understand why you posting there
2. As expected your are completely ignoring that parseDouble or valueOf can actually trow NumberFormatException.

Edited by peter_budo: n/a

0

1. Your above post was moved from Java to Mobile Development, I do not understand why you posting there
2. As expected your are completely ignoring that parseDouble or valueOf can actually trow NumberFormatException.

How should i go about coding it so that i do not end up with a NumberFormatException?

Edited by nerdy9000: n/a

0

Well either you are entering non-numeric value and that is triggering exception or you are not able retrieve text input therefore input.getText().toString return null and that cause exception. Since you did not post enough code I can't say for sure what is wrong

0

I have tried to just output the number as a string without trying to convert it to a a double and it worked.

So that means the input.getText().toString is not null.

Here is my code.

package mashuda.dan.converter;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ConverterActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
       
        final double number;
        
        Spinner unit = (Spinner) findViewById(R.id.unitSelect);
        final EditText input = (EditText) findViewById(R.id.userNumber);
        Button convertButton = (Button) findViewById(R.id.convertButton);
         final TextView output = (TextView) findViewById(R.id.output);
        
        
        
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.unit_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        unit.setAdapter(adapter);
        
        
   
        
        convertButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {
            
            	number = Double.valueOf( input.getText().toString() );  
            	output.setText(" " +number);

            }
        });
        
        
    }
}
0

Solution 1, use try and catch as mentioned previosly

private double Doublify(EditText editText){
     try {
       return Double.parseDouble(input.getText().toString());
     } catch (NumberFormatException e) {
        return 0;
     }
 }

Solution 2, configure EditText to accept numbers only

<EditText android:inputType="numberDecimal" />

by declaring inputType

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.