0

When I try running my Android App, I get an error saying: java.lang.NumberFormatException: Invalid double: "". This is my code:

public class MainActivity extends Activity {

double score;
EditText gpa;
EditText sat;
EditText act;
Button calc;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    gpa = (EditText) findViewById(R.id.gpa);
    String gpaString = gpa.getText().toString();
    final double gpaDouble = Double.parseDouble(gpaString);

    sat = (EditText) findViewById(R.id.sat);
    String satString = sat.getText().toString();
    final int satInt = Integer.parseInt(satString);

    act = (EditText) findViewById(R.id.act);
    String actString = act.getText().toString();
    final int actInt = Integer.parseInt(actString);


    calc = (Button) findViewById(R.id.calc);

    calc.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(actInt/36>satInt/2400){
                score = (0.6*gpaDouble*25)+(0.4*(actInt/36)*100);
            }else{
                score = (0.6*gpaDouble*25)+(0.4*(satInt/2400)*100);
            }


        }
    });
}


}

I essentially want to get numbers from three EditTexts, make one of them into a double and the other two into ints. Then I would use these variables to set the value for another double variable. I am not getting errors before I run the app. I feel that when the EditText field is blank, it will not parse correctly, but I am unsure how to solve this. What is the problem?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
RainbowJeremy
  • 63
  • 4
  • 13

1 Answers1

1
Invalid double: "".

you are parsing onCreate() value without putting any default value so the exception

final double gpaDouble = Double.parseDouble(gpaString);

because "" (empty String is not Double)

jmj
  • 237,923
  • 42
  • 401
  • 438