-3

Hey guys I would really like to calculate not only the c=hypotenuse but also a or b out from what was given in the 3 EditText-Views. But it crashes and won't output a result. Any suggestions? I'm pretty new to this stuff. :)

package net.starostka.somefunctions;

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

public class Pythagoras extends AppCompatActivity implements View.OnClickListener {

EditText aNumPyt;
EditText bNumPyt;
EditText cNumPyt;

Button pythagorasCalcu;

TextView pythagorasResultFinal;

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

    aNumPyt = (EditText) findViewById(R.id.aNumPyt);
    bNumPyt = (EditText) findViewById(R.id.bNumPyt);

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

    pythagorasResultFinal = (TextView) findViewById(R.id.pythagorasResultFinal);

    // set a listener
    pythagorasCalcu.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    double a=0,b=0,c=0;
    double hypot = 0.0;

    /*
    // check if the fields are empty
    if (TextUtils.isEmpty(aNumPyt.getText().toString()) || TextUtils.isEmpty(bNumPyt.getText().toString()) || TextUtils.isEmpty(cNumPyt.getText().toString())) {
        return;
    }
    */

    // read EditText and fill variables with numbers
    a = Double.parseDouble(aNumPyt.getText().toString());
    b = Double.parseDouble(bNumPyt.getText().toString());
    c = Double.parseDouble(cNumPyt.getText().toString());

    if (a>b && a>c){
        hypot=a;
        if(hypot*hypot==(b*b+c*c)){
            //result = Math.sqrt(Math.pow(a,2)+Math.pow(b,2));
            pythagorasResultFinal.setText(String.valueOf(hypot));
        }
    } else if (b>a && b>c){
        hypot=b;
        if(hypot*hypot==(a*a+c*c)){
            //result = Math.sqrt(Math.pow(a,2)+Math.pow(b,2));
            pythagorasResultFinal.setText(String.valueOf(hypot));
        }
    } else if (c>a && c>b){
        hypot=c;
        if(hypot*hypot==(a*a+b*b)){
            //result = Math.sqrt(Math.pow(a,2)+Math.pow(b,2));
            pythagorasResultFinal.setText(String.valueOf(hypot));
        }
    }
}

}

The XML file

    </LinearLayout>
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginLeft="5pt"
        android:layout_marginRight="5pt"
        android:textSize="12pt"
        android:layout_marginTop="3pt"
        android:id="@+id/pythagorasResultFinal"
        android:gravity="center_horizontal">
    </TextView>

Before crash:

After crash:

Biruk Abebe
  • 2,235
  • 1
  • 13
  • 24
strka
  • 3
  • 3
  • 2
    Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare May 14 '16 at 14:10

3 Answers3

4

EDIT Interesting that no one pointed this out earlier, but I just realized you never initialize cNumPyt. You initialize the other EditTexts but never C. So when you try to get the text, you get NullPointerException because you try to get information from nothing.

Initialize the correct text box in your onCreate method.


In your image, you show that you are leaving input blank for one of the text boxes (in the "after crash" image, we see that you entered 3, 4, and then nothing). This means that there is no Double to be parsed, because "" is not a number.

From the Double documentation:

public static double parseDouble(String s) throws NumberFormatException
[...]
Throws:
NullPointerException - if the string is null
NumberFormatException - if the string does not contain a parsable double.

If your input is blank, you'll get an exception. Since it's not in a try-catch block, your application will crash.

To be more specific:

//cNumPyt is empty, and "" is not a parseable number, so NumberFormatException
c = Double.parseDouble(cNumPyt.getText().toString());

Sample block (you will need to do this for every variable separately so you can tell which variable was empty):

try{
    a = Double.parseDouble(aNumPyt.getText().toString());
}catch (NumberFormatException e){
    //oops, no number available
    a = 0; //default to 0 (or whatever you choose)
}
Arc676
  • 4,445
  • 3
  • 28
  • 44
  • Hmm that makes sense, i tried using your sample to print what letter was missing instead of crashing.. But it still won't work try{ a = Double.parseDouble(aNumPyt.getText().toString()); }catch (NumberFormatException e){ //oops, no number available a = 0; //default to 0 (or whatever you choose) pythagorasResultFinal.setText("No A input"); } Btw: Thanks for the REALLY fast response – strka May 14 '16 at 14:45
  • @BenjaminStarostkaJakobsen Could you be more specific than "won't work"? As in it still crashes? Did you use separate `try` blocks for `a`, `b`, _and_ `c`? – Arc676 May 14 '16 at 14:48
  • Yes of course, sorry. It still crashes was what I meant. Here is a Pic: http://imgur.com/AGOF7Yf – strka May 14 '16 at 14:52
2
 @Override
public void onClick(View v) {
double a=0,b=0,c=0;


if (!TextUtils.isEmpty(aNumPyt.getText().toString()) &&
    !TextUtils.isEmpty(bNumPyt.getText().toString()) &&
    !TextUtils.isEmpty(cNumPyt.getText().toString())) 

{ 

a = Double.parseDouble(aNumPyt.getText().toString());
b = Double.parseDouble(bNumPyt.getText().toString());
c = Double.parseDouble(cNumPyt.getText().toString());


    if((a*a)==((b*b)+(c*c))){
        pythagorasResultFinal.setText(String.valueOf(a));
    }
    else if ((b*b)==((a*a)+(c*c))){
        pythagorasResultFinal.setText(String.valueOf(b));
    }
    else if ((c*c)==((a*a)+(b*b))){
        pythagorasResultFinal.setText(String.valueOf(c));
    }
}

}
Mrugesh
  • 4,381
  • 8
  • 42
  • 84
  • I tried your solution but the problem still seems to lay in the NumberFormatException as Arc676 explained... It still crashes even if I put a 0 in the empty input field. :/ – strka May 14 '16 at 15:30
  • did you put android:inputType="numberDecimal" in your xml file? – Mrugesh May 14 '16 at 15:41
0

I found the problem.. Arc676's solution worked as seen in the code below:

@Override
public void onClick(View v) {
    double a=0.0,b=0.0,c=0.0;
    double hypot = 0.0;

    try{
        a = Double.parseDouble(aNumPyt.getText().toString());
    }catch (NumberFormatException e){
        //oops, no number available
        a = 0.0; //default to 0 (or whatever you choose)
    }
    try{
        b = Double.parseDouble(bNumPyt.getText().toString());
    }catch (NumberFormatException e){
        //oops, no number available
        b = 0.0; //default to 0 (or whatever you choose)
    }
    try{
        c = Double.parseDouble(cNumPyt.getText().toString());
    }catch (NumberFormatException e){
        //oops, no number available
        c = 0.0; //default to 0 (or whatever you choose)
    }

    if (c == 0.0){
        hypot = Math.sqrt((Math.pow(a,2))+(Math.pow(b,2)));
        pythagorasResultFinal.setText("Finding C\n" + String.valueOf(hypot));
    }else if (a == 0.0){
        hypot = Math.sqrt((Math.pow(c,2))-(Math.pow(b,2)));
        pythagorasResultFinal.setText("Finding A\n" + String.valueOf(hypot));
    }else if (b == 0.0) {
        hypot = Math.sqrt((Math.pow(c,2))-(Math.pow(a,2)));
        pythagorasResultFinal.setText("Finding B\n" + String.valueOf(hypot));
    }
}

Another essential problem i found was i did not define the id for the c View.. Really small problem causing it all to crash..

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

    aNumPyt = (EditText) findViewById(R.id.aNumPyt);
    bNumPyt = (EditText) findViewById(R.id.bNumPyt);
    cNumPyt = (EditText) findViewById(R.id.cNumPyt); //Forgot to add this line of code

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

    pythagorasResultFinal = (TextView) findViewById(R.id.pythagorasResultFinal);

    // set a listener
    pythagorasCalcu.setOnClickListener(this);
}

Thanks for the fast response and help! (y)

strka
  • 3
  • 3