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");
}
}
});
}
}