954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Andorid Java parseDouble Issue

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?

nerdy9000
Newbie Poster
6 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

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

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 
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."

nerdy9000
Newbie Poster
6 posts since Aug 2011
Reputation Points: 10
Solved Threads: 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);

            }
        });
nerdy9000
Newbie Poster
6 posts since Aug 2011
Reputation Points: 10
Solved Threads: 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.

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 
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?

nerdy9000
Newbie Poster
6 posts since Aug 2011
Reputation Points: 10
Solved Threads: 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

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

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

            }
        });
        
        
    }
}
nerdy9000
Newbie Poster
6 posts since Aug 2011
Reputation Points: 10
Solved Threads: 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

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

It works. Thanks for your help

nerdy9000
Newbie Poster
6 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: