Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
115 views
in Technique[技术] by (71.8m points)

android - java.lang.numberformatexception: invalid double: " "

getting an error of invalid Double java.lang.numberformatexception invalid double: "" what is the reason for this

Activity 1

package com.example.solarcalculator;

        import android.os.Bundle;
        import android.widget.Button;
        import android.annotation.SuppressLint;
        import android.app.Activity;
        import android.view.Menu;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.EditText;
        import android.app.AlertDialog;
        import android.content.Intent;

        @SuppressLint("UseValueOf")
        public class MainActivity extends Activity {
            private EditText input1;
            private EditText input2;
            private EditText input3;
            private EditText input4;
            private EditText input5;
            private MainActivity mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_main);

        input5 = (EditText) findViewById(R.id.input5);
        Button button1 = (Button) findViewById(R.id.button1);
        input4 = (EditText) findViewById(R.id.input4);
        input1 = (EditText) findViewById(R.id.input1);
        input2 = (EditText) findViewById(R.id.input2);
        input3 = (EditText) findViewById(R.id.input3);
        button1.setOnClickListener(new OnClickListener() {

            @SuppressWarnings("unused")
            private AlertDialog show;

            @SuppressLint("UseValueOf")
            @Override
            public void onClick(View arg0) {
                if (  (input4.getText().toString() == " ") 
                        || (input4.getText().length() ==0) ||
                                                (input5.getText().length() == 0)                         
                        || (input5.getText().toString() == " ")){
                show = new      AlertDialog.Builder(mContext).setTitle("Error")
                            .setMessage("Some inputs are empty")
                            .setPositiveButton("OK", null).show();
                }
    else if ((input1.getText().length() != 0) &&
        (input3.getText().length() ==0) && (input2.getText().length() ==    0)){
                     double w = new Double(input3.getText().toString());
                     double t = new Double(input4.getText().toString());
                     double x = new Double(input5.getText().toString());
                     float e = 7;
                     double num = 1000*x;
                     double den = w*t*e;
                     double payback = num/den;
                     double money = w*t*e/1000;
         Intent intent = new Intent(MainActivity.this, Power.class);
                     intent.putExtra("payback", payback);
                     intent.putExtra("money", money);
                     startActivity(intent);

                }
    else if ((input1.getText().length() == 0) &&   (input3.getText().length() != 0) &&
                                (input2.getText().length() != 0)){
                     double t = new    
                                     Double(input4.getText().toString());
                     double x = new Double(input5.getText().toString());
                     double v = new Double(input2.getText().toString());
                     double i = new Double(input3.getText().toString());
                     float e = 7;
                     double num = 1000*x;
                     double den = v*i*t*e;
                     double payback = num/den;
                     double money = v*i*t*e/1000;
             Intent intent = new Intent(MainActivity.this, Power.class);
                     intent.putExtra("payback", payback);
                     intent.putExtra("money", money);
                     startActivity(intent);

                }
                else {
                    double t = new Double(input4.getText().toString());
                     double x = new Double(input5.getText().toString());
                     double v = new Double(input2.getText().toString());
                     double i = new Double(input3.getText().toString());
                     float e = 7;
                     double num = 1000*x;
                     double den = v*i*t*e;
                     double payback = num/den;
                     double money = v*i*t*e/1000;
               Intent intent = new Intent(MainActivity.this, Power.class);
                     intent.putExtra("payback", payback);
                     intent.putExtra("money", money);
                     startActivity(intent);

                }
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

       }

Activity2

package com.example.solarcalculator;

    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;
   @SuppressLint("NewApi")
     public class Power extends Activity {

private double money;
private double payback;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.power);
    payback = getIntent().getDoubleExtra("payback",0);
    money = getIntent().getDoubleExtra("money", 0);
    TextView pay = (TextView) findViewById(R.id.textView2);
    String payback1 = Double.toString(payback);
    pay.setText(payback1);
    TextView mon = (TextView) findViewById(R.id.textView4);
    String money1 = Double.toString(money);
    mon.setText(money1);

     }
    }

I am getting java.lang.numberformatexception invalid double: "" error in logcat anyone please help

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The reason is, that "" is not a valid double. You need to test the String before or catch such exceptions

double w;

try {
    w = new Double(input3.getText().toString());
} catch (NumberFormatException e) {
    w = 0; // your default value
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...