0

So basically I have a problem. When the user presses the calculate button (I'm making a calculator) without entering a value the app crashes. I want it to display a message instead but I don't know how. (updated)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.waist2height); {
        //h is cm edittext 
        final EditText h = (EditText)findViewById(R.id.editText2);
        final EditText w = (EditText)findViewById(R.id.editText3);

        final EditText r = (EditText)findViewById(R.id.textView4);

    });

        calculate.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub

            float height=0;
            float weight=0;
            float result=0;



            result = (float) (( weight / height ) + 0.005) ;

            if ((h.getText().toString()).equals(""))
                {
                   Toast.makeText(getApplicationContext(),"Please enter your height", Toast.LENGTH_SHORT).show();
                   float height = Float.parseFloat(h.getText().toString());
                }
                if ((w.getText().toString()).equals(""))
                {
                   Toast.makeText(getApplicationContext(),"Please enter your weight", Toast.LENGTH_SHORT).show();
                   float weight = Float.parseFloat(w.getText().toString());
                }


            r.setText(String.valueOf(result));
            }

the code below is where i have the problem

if ((h.getText().toString()).equals(""))
                {
                   Toast.makeText(getApplicationContext(),"Please enter your height", Toast.LENGTH_SHORT).show();
                   float height = Float.parseFloat(h.getText().toString());
                }
                if ((w.getText().toString()).equals(""))
                {
                   Toast.makeText(getApplicationContext(),"Please enter your weight", Toast.LENGTH_SHORT).show();
                   float weight = Float.parseFloat(w.getText().toString());
                }

This is the best I can come up with but it doesn't work.

Here is the LogCat (Updated)

    09-20 18:50:38.509: E/AndroidRuntime(29769): FATAL EXCEPTION: main
09-20 18:50:38.509: E/AndroidRuntime(29769): Process: com.NovaPlex.personalfitnessfree, PID: 29769
09-20 18:50:38.509: E/AndroidRuntime(29769): java.lang.NumberFormatException: Invalid float: ""
09-20 18:50:38.509: E/AndroidRuntime(29769):    at java.lang.StringToReal.invalidReal(StringToReal.java:63)
09-20 18:50:38.509: E/AndroidRuntime(29769):    at java.lang.StringToReal.parseFloat(StringToReal.java:308)
09-20 18:50:38.509: E/AndroidRuntime(29769):    at java.lang.Float.parseFloat(Float.java:306)
09-20 18:50:38.509: E/AndroidRuntime(29769):    at com.NovaPlex.personalfitnessfree.WH$2.onClick(WH.java:80)
09-20 18:50:38.509: E/AndroidRuntime(29769):    at android.view.View.performClick(View.java:4780)
09-20 18:50:38.509: E/AndroidRuntime(29769):    at android.view.View$PerformClick.run(View.java:19866)
09-20 18:50:38.509: E/AndroidRuntime(29769):    at android.os.Handler.handleCallback(Handler.java:739)
09-20 18:50:38.509: E/AndroidRuntime(29769):    at android.os.Handler.dispatchMessage(Handler.java:95)
09-20 18:50:38.509: E/AndroidRuntime(29769):    at android.os.Looper.loop(Looper.java:135)
09-20 18:50:38.509: E/AndroidRuntime(29769):    at android.app.ActivityThread.main(ActivityThread.java:5254)
09-20 18:50:38.509: E/AndroidRuntime(29769):    at java.lang.reflect.Method.invoke(Native Method)
09-20 18:50:38.509: E/AndroidRuntime(29769):    at java.lang.reflect.Method.invoke(Method.java:372)
09-20 18:50:38.509: E/AndroidRuntime(29769):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
09-20 18:50:38.509: E/AndroidRuntime(29769):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Ashraf96
  • 53
  • 1
  • 3
  • 17

4 Answers4

1

Don't use == null. Assuming you've done findviewbyid, it cannot be null. It doesn't check for contents.

This is a simple method how to find if EditText is empty. Parameter is the EditText you want to check and it returns true for empty / false for non-empty.

private boolean isEmpty(EditText etText) {
    return etText.getText().toString().trim().length() <= 0;
}

EDIT :

by using android:id="@+id/someId in xml files, we tell Android to create new resource with chosen id (in this case someId) in our R.java class. When doing findviewbyid we assign that value to our View.

In short if OP did something like EditText EditText1 = (EditText) findViewById(R.id.myFistEditText); before and he later tries to compare it in his if like (EditText1 == null), it's not actually getting value of text in his EditText, it's just telling him that - yes, this view was found before and it's assigned.

poss
  • 1,759
  • 1
  • 12
  • 27
  • _"Assuming you've done findviewbyid, it cannot be null"_... Care to explain? – Onik Sep 13 '15 at 18:32
  • Again, this statement may not be correct. The method may return null in some cases (see [findViewByID returns null](http://stackoverflow.com/questions/3264610/findviewbyid-returns-null)). So, checking if it's null isn't redundant. – Onik Sep 13 '15 at 19:13
  • It does nothing about checking for contents of the edittext, which is the main point of the question. The edge case of `onFinishInflate()` is in my opinion quite irrelevant because there is no point in checking EditText contents if the view wasnt inflated in the first place. – poss Sep 13 '15 at 19:18
1

You can just try this, to check if value is not entered in the EditText.

if ((EditText1.getText().toString()).equals(""))
{
   Toast.makeText(getApplicationContext(),"Please enter Value1", Toast.LENGTH_LONG).show();
}
if ((EditText2.getText().toString()).equals(""))
{
   Toast.makeText(getApplicationContext(),"Please enter Value2", Toast.LENGTH_LONG).show();
}

Alternatively, you can use .matches("") instead of .equals("")

UPDATE

You have to do it like this

if (!(h.getText().toString()).equals(""))
{
   height= Float.parseFloat(h.getText().toString());
}

and

if (!(w.getText().toString()).equals(""))
{
   weight= Float.parseFloat(w.getText().toString());
}

UPDATE

if ((EditText1.getText().toString()).equals(""))
{
   Toast.makeText(getApplicationContext(),"Please enter Value1", Toast.LENGTH_LONG).show();
}
if ((EditText2.getText().toString()).equals(""))
{
   Toast.makeText(getApplicationContext(),"Please enter Value2", Toast.LENGTH_LONG).show();
}
if (!(h.getText().toString()).equals(""))
{
   height= Float.parseFloat(h.getText().toString());
}
if (!(w.getText().toString()).equals(""))
{
   weight= Float.parseFloat(w.getText().toString());
}

Replace the above code instead of yours

Lal
  • 14,726
  • 4
  • 45
  • 70
0

It is already said that you'd better write a method isEmpty.

My suggestion is rather than writing isEmpty write a method called isValid which checks if the value is a desired value i.e. integer, double etc.

This way your program wont crash when a string is entered.

smttsp
  • 4,011
  • 3
  • 33
  • 62
0

use

if(textView.getText().toString().isEmpty()){
      Toast.makeText(this , "Enter A Number" , Toast.LENGTH_SHORT).show();
}
Dracula
  • 9
  • 2
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – codedge Jan 23 '21 at 12:57