Search code examples
androidquadratic

Quadratic formula calculation in android?


Hello guys I m new to android and want to display the quadratic formula calculation here is the code i am doing in android there is no error showing in this code but still on the emulator its not displaying the result :-(

public class MainActivity extends Activity {

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

    Button b1 = (Button) findViewById(R.id.btnSubmit);
    final EditText et1 = (EditText) findViewById(R.id.etVA);//whats wrong
    final EditText et2 = (EditText) findViewById(R.id.etVC);//whats wrong
    final EditText et3 = (EditText) findViewById(R.id.etVC);//whats wrong
    final TextView answer = (TextView) findViewById(R.id.txtResult);//whats wrong
    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            String a1 = et1.getText().toString();//whats wrong
            double a = Double.parseDouble(a1);//whats wrong

            String b1 = et2.getText().toString();//whats wrong
            double b = Double.parseDouble(b1);//whats wrong

            String c1 = et3.getText().toString();//whats wrong
            double c = Double.parseDouble(c1);//whats wrong

            double x1 = (-b+Math.sqrt(b*b-4*a*c))/(2*a);//whats wrong

            System.out.println("x1 = "+ x1);

            String R = x1+ "";  //whats wrong
            answer.setText("Your Answer is " + R); //whats wrong
        }
    });
}

Solution

  • I have found the solution. Code was fine but I was only doing a simple logical mistake

    final EditText et1 = (EditText) findViewById(R.id.etVA); 
    final EditText et2 = (EditText) findViewById(R.id.etVC);//that is the mistake it should be like (R.id.etVB).... 
    final EditText et3 = (EditText) findViewById(R.id.etVC);