-2

When the input is invalid (empty, . , etc, it crashes.Any ideas?

I've tried various ways to re-arrange the code but i couldn't make it work. When valid input is introduced the app works fine

public class fourth extends AppCompatActivity {


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


        final Button calc = (Button) findViewById(R.id.calculeaza);
        final EditText e1 = (EditText) findViewById(R.id.inaltime);
        final EditText e2 = (EditText) findViewById(R.id.greutate);
        final TextView t1 = (TextView) findViewById(R.id.rezultat);
        calc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                String text = e1.getText().toString();
                String text2 = e2.getText().toString();


                if (text == ( "," ) || text == ( "" ) || text == ( "-" ) || text2 == ( "," ) || text2 == ( "" ) || text2 == ( "-" ))
                {

                    float num1 = Float.parseFloat(e1.getText().toString());
                    float num2 = Float.parseFloat(e2.getText().toString());
                    float sum = ( num2 / ( ( num1 * num1 ) / 10000 ) );
                    t1.setText(Float.toString(sum));

                } else {
                    t1.setText("Invalid");


                }

            }


        });
    }
}
lightS
  • 3
  • 3
  • First, to compare String, use `equals` not `==`. Next, you try to convert a string to a float only when it's an empty string, a coma or a minus sign. How do you expect that to work? – jhamon Oct 03 '16 at 11:58
  • hint: reverse your condition – jhamon Oct 03 '16 at 12:13
  • Define 'input', 'valid', 'invalid','crashes', ... Then have a think about side issues like why you're calling `String,toString()`. – user207421 Oct 03 '16 at 12:36

1 Answers1

0

First of all. Your string comparison is wrong. You should use String#equals

Thus:

if (text.equals(",") || text.equals("") || text.equals("-") || text2.equals(",") || text2.equals("") || text2.equals("-"))

You have not specified the error you get. However my guess is that the error occurs on the lines where you parse your text to Floats. It makes no sense to do that if text is ,.

Community
  • 1
  • 1
Jurriaan
  • 71
  • 7
  • thank you all for feedback, I'm obviously noob in coding and i have much to learn. the reversal of the condition worked, also had some typing errors, also the result code was wrong - t1.setText(String.valueof(sum)) worked just fine have a nice day guys!!! (bow) – lightS Oct 03 '16 at 13:32
  • Good to hear it worked. Would you mind tagging the correct answer? This may help future google searches ;) – Jurriaan Oct 03 '16 at 15:14
  • Here is the working code (BMI calculator) https://github.com/hackingteam55/AV/blob/master/fourth.java – lightS Oct 04 '16 at 09:51
  • Good to hear it works! I actually recommend changing your code slightly. The [Float#parseFloat(String)](https://docs.oracle.com/javase/7/docs/api/java/lang/Float.html#parseFloat(java.lang.String)) throws a `NumberFormatException` if the parsed text is invalid. You could put the 2 parses in a try block and catch this exception. This is much safer in terms of preventing runtime exceptions. [Something like this(untested)](http://pastebin.com/GCiq1xVX) – Jurriaan Oct 05 '16 at 11:48