I would like to make it so that when the editText fields are empty, no result is shown instead of the app crashing.
I read over this result here: How to break out or exit a method in Java?
as well as some other results based on whether the number was negative or positive. I've been working in this for about an hour and I'm throwing in the towel. I'm new to this, and I think I must just not be putting the if statement in the correct location?
Here's the code. Any pointers in the right direction would be helpful. Thanks.
public class incomePage extends AppCompatActivity {
EditText perYearAmount;
EditText perMonthAmount;
EditText perWeekAmount;
Button totalButton;
TextView addResult;
double year, month, week, sum;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_income_page);
perYearAmount = (EditText) findViewById(R.id.perYearAmount);
perMonthAmount = (EditText) findViewById(R.id.perMonthAmount);
perWeekAmount = (EditText) findViewById(R.id.perWeekAmount);
totalButton = (Button) findViewById(R.id.totalButton);
addResult = (TextView) findViewById(R.id.totalMonthlyIncome);
totalButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
year = Double.parseDouble(perYearAmount.getText().toString());
month = Double.parseDouble(perMonthAmount.getText().toString());
week = Double.parseDouble(perWeekAmount.getText().toString());
sum = year + month + week;
addResult.setText(Double.toString(sum));
if (perYearAmount.getText().toString().equals("")) {
return;
}
if (perMonthAmount.getText().toString().equals("")) {
return;
}
if (perWeekAmount.getText().toString().equals("")) {
return;
}
if (totalButton.getText().toString().equals("")) {
return;
}
if (addResult.getText().toString().equals("")) {
return;
}
}
});
}
}
Here's the logCat error. You guys are fast!!
08-24 15:49:31.799 15154-16038/com.example.android.budgeit10 D/OpenGLRenderer: Enabling debug mode 0
08-24 15:49:35.123 15154-16038/com.example.android.budgeit10 D/OpenGLRenderer: endAllStagingAnimators on 0xb8937048 (RippleDrawable) with handle 0xb897a910
08-24 15:49:36.645 15154-16038/com.example.android.budgeit10 D/OpenGLRenderer: endAllStagingAnimators on 0xb89e95a0 (RippleDrawable) with handle 0xb89ebac8
08-24 15:49:50.694 15154-15154/com.example.android.budgeit10 D/AndroidRuntime: Shutting down VM
08-24 15:49:50.695 15154-15154/com.example.android.budgeit10 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.budgeit10, PID: 15154
java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:267)
at java.lang.Double.parseDouble(Double.java:301)
at com.example.android.budgeit10.incomePage$1.onClick(incomePage.java:34)
at android.view.View.performClick(View.java:4785)
at android.view.View$PerformClick.run(View.java:19884)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)