0

I have two issues (that I'm aware of) on this calculation program (taking several inputs, run calculation, several outputs). Thanks for any help!!

1) Variable 'aResult' initializer 'Double.parseDouble(tvaResult.getText().toString()) is redundant similar warning on cResult etResult beta1Result phiResult MnResult & phiMnResult

2) Application is crashing after clicking the button. This leads me to believe that either some of my calculations are running into errors, or the button coding itself is incorrect.

--------- beginning of crash E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.ericallenbellville.rcbeamdesign, PID: 3087 java.lang.NumberFormatException: empty String at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1071) at java.lang.Double.parseDouble(Double.java:547) at com.example.ericallenbellville.rcbeamdesign.MainActivity.onClick(MainActivity.java:54) at android.view.View.performClick(View.java:5637) at android.view.View$PerformClick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

    package com.example.ericallenbellville.rcbeamdesign;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    public Button btnCalc;
    private TextView tvaResult;
    private TextView tvcResult;
    private TextView tvetResult;
    private TextView tvphiResult;
    private TextView tvMnResult;
    private TextView tvphiMnResult;
    private TextView tvbeta1Result;
    private EditText etB,etD,etAs,etFc,etFy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        btnCalc = (Button)findViewById(R.id.btnCalc);
        etB = (EditText)findViewById(R.id.etB);
        etD = (EditText)findViewById(R.id.etD);
        etAs = (EditText)findViewById(R.id.etAs);
        etFc = (EditText)findViewById(R.id.etFc);
        etFy = (EditText)findViewById(R.id.etFy);
        tvaResult = (TextView)findViewById(R.id.tvaResult);
        tvcResult = (TextView)findViewById(R.id.tvcResult);
        tvetResult = (TextView)findViewById(R.id.tvetResult);
        tvphiResult = (TextView)findViewById(R.id.tvphiResult);
        tvMnResult = (TextView)findViewById(R.id.tvMnResult);
        tvphiMnResult = (TextView)findViewById(R.id.tvphiMnResult);
        tvbeta1Result = (TextView)findViewById(R.id.tvbeta1Result);

        btnCalc.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
            Double B = Double.parseDouble(etB.getText().toString());
            Double D = Double.parseDouble(etD.getText().toString());
            Double As = Double.parseDouble(etAs.getText().toString());
            Double Fc = Double.parseDouble(etFc.getText().toString());
            Double Fy = Double.parseDouble(etFy.getText().toString());
            Double aResult = Double.parseDouble(tvaResult.getText().toString());
            Double cResult = Double.parseDouble(tvcResult.getText().toString());
            Double etResult = Double.parseDouble(tvetResult.getText().toString());
            Double beta1Result = Double.parseDouble(tvbeta1Result.getText().toString());
            Double phiResult = Double.parseDouble(tvphiResult.getText().toString());
            Double MnResult = Double.parseDouble(tvMnResult.getText().toString());
            Double phiMnResult = Double.parseDouble(tvphiMnResult.getText().toString());
            switch(view.getId()) {
                case R.id.btnCalc:
                    if (Fc <= 4000) {
                        beta1Result = (0.85);
                    } else if (4000 < Fc && Fc <= 8000) {
                        beta1Result = ((0.85)-(0.05 * ((Fc - 4000) / (1000))));
                    } else {
                        beta1Result = 0.65;
                    }
                    aResult = ((Fy * As) / (0.85 * Fc * B));
                    cResult = (aResult / beta1Result);
                    etResult = (((D - cResult) / (cResult)) * 0.003);
                    if (etResult >= 0.005) {
                        phiResult = (0.9);
                    } else if (0.002 <= etResult && etResult < 0.005) {
                        phiResult = (0.65 + (etResult - 0.002) * 0.25 / (0.005 - 0.002));
                    } else {
                        phiResult = (0.00);
                    }
                    MnResult = (((Fy * As) * (D - (aResult / 2.0))));
                    phiMnResult = phiResult * MnResult;
                    tvaResult.setText(String.valueOf(aResult));
                    tvcResult.setText(String.valueOf(cResult));
                    tvetResult.setText(String.valueOf(etResult));
                    tvphiResult.setText(String.valueOf(phiResult));
                    tvMnResult.setText(String.valueOf(MnResult));
                    tvphiMnResult.setText(String.valueOf(phiMnResult));
                    break;

            }}
    }
  • Post your stack trace. – denvercoder9 Dec 02 '16 at 04:29
  • @RafiduzzamanSonnet Do you mean the crash log? I just posted that. Sorry, this is 1st program I've ever needed to write. – Eric Bellville Dec 02 '16 at 04:38
  • You are trying to parse an empty string in a Double. – denvercoder9 Dec 02 '16 at 04:45
  • @RafiduzzamanSonnet Is this in reference to the 1st issue? Any idea how to fix this? If I Remove the lines then it says they aren't Initialized – Eric Bellville Dec 02 '16 at 04:45
  • On line 54 in MainActivity.java. Just use your app to debug. When you click the button, are there any EditText's that are empty? – denvercoder9 Dec 02 '16 at 04:46
  • @RafiduzzamanSonnet All EditTexts are full, but there are TextView that are empty when the button is clicked there are the Results that are empty, this is what lines 54-60 are referring to. However, if I remove them it causes issues saying that they aren't Initialized, then I can't use them in the calculations. – Eric Bellville Dec 02 '16 at 04:49

1 Answers1

0

You are not using the values, aResult, cResult, etc until you calculate them inside the switch statement. So why initialize them using the text from the textviews? The reason you are getting an exception, is because the textviews are empty and you are trying to parse empty string into a Double.

double aResult = 0; //Double.parseDouble(tvaResult.getText().toString());
double cResult = 0; //Double.parseDouble(tvcResult.getText().toString());
double etResult = 0; //Double.parseDouble(tvetResult.getText().toString());
double beta1Result = 0; //Double.parseDouble(tvbeta1Result.getText().toString());
double phiResult = 0; //Double.parseDouble(tvphiResult.getText().toString());
double MnResult = 0; //Double.parseDouble(tvMnResult.getText().toString());
double phiMnResult = 0; //Double.parseDouble(tvphiMnResult.getText().toString());
switch(view.getId()) {
    case R.id.btnCalc:
    if (Fc <= 4000) {
        beta1Result = (0.85);
    } else if (4000 < Fc && Fc <= 8000) {
        beta1Result = ((0.85)-(0.05 * ((Fc - 4000) / (1000))));
    } else {
        beta1Result = 0.65;
    }
    aResult = ((Fy * As) / (0.85 * Fc * B));
    cResult = (aResult / beta1Result);
    etResult = (((D - cResult) / (cResult)) * 0.003);
    if (etResult >= 0.005) {
        phiResult = (0.9);
    } else if (0.002 <= etResult && etResult < 0.005) {
        phiResult = (0.65 + (etResult - 0.002) * 0.25 / (0.005 - 0.002));
    } else {
        phiResult = (0.00);
    }
    MnResult = (((Fy * As) * (D - (aResult / 2.0))));
    phiMnResult = phiResult * MnResult;
    tvaResult.setText(String.valueOf(aResult));
    tvcResult.setText(String.valueOf(cResult));
    tvetResult.setText(String.valueOf(etResult));
    tvphiResult.setText(String.valueOf(phiResult));
    tvMnResult.setText(String.valueOf(MnResult));
    tvphiMnResult.setText(String.valueOf(phiMnResult));
    break;
}
denvercoder9
  • 2,979
  • 3
  • 28
  • 41
  • I realized what you were saying before this answer. I deleted the rest of the text relating to double.parse..... on the Results. I checked this and the calculation ran to completion. I believe your way of setting them to 0 would be the same effect. Does that sound about right? – Eric Bellville Dec 02 '16 at 05:06
  • Yes, it'd be the same. BTW, you are now declaring those variables inside the `switch` case, right? – denvercoder9 Dec 02 '16 at 05:08
  • At the end of the code the Results are translated back into the TextView (unless I'm misunderstanding something) – Eric Bellville Dec 02 '16 at 05:08
  • I declared them outside the switch, I can test them inside to see if it changes anything. – Eric Bellville Dec 02 '16 at 05:09
  • Nah, it's okay. Yes, you are right. The values will be added to the textview – denvercoder9 Dec 02 '16 at 05:10
  • Another issue I came up with is that the results come out to very long decimal points. Is there a setting in Android Studio that I should look for to set a # of decimals? – Eric Bellville Dec 02 '16 at 05:13
  • That is a different question than the original post. But here you go: http://stackoverflow.com/questions/4885254/string-format-to-format-double-in-java – denvercoder9 Dec 02 '16 at 05:15