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?

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

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."

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);

            }
        });

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.

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?

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

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);

            }
        });
        
        
    }
}

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

It works. Thanks for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.