• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Cannot proccess math? (Force Closes)

darkwispr

Newbie
Hello Android Devs,
I'm making an app (my first app that does something) that calculates some electric basic things, thing is when I press the button it force closes the app.

(I made some code that has some Java math things and have not imported anything that has to do with calculation. I've tried putting it in the main activity, putting it in the Activity its running, changing some of the code below, still doesn't work.)
Code:
    public void getResult (View v){
        int orbit, result;
        String putresult;
        orbit =  Integer.parseInt(findViewById(R.id.number).toString());
        result = 2*orbit^2;
        
        putresult = String.valueOf(result);
        EditText et = (EditText) findViewById(R.id.atom_result);
        et.setText(putresult);
    }
I don't know if some of the stuff above is impossible, I'm not asking for the code, asking for the correct way to do it.
 
I'll try to answer your questions as best I can.

1) What kind of View is R.id.number? I am assuming it's an EditText? You should cast to EditText and get the text like this:

Code:
EditText numberEditText = (EditText) findViewById(R.id.number);
orbit =  Integer.parseInt(numberEditText.getText().toString());

Also, the thing that is probably giving you the biggest issue is that '^' probably doesn't do what you think. In java, '^' is not the exponent operator, but the bitwise exclusive OR operator. In fact, in Java there is no operator for exponents. Instead, there is a static function pow(double, double) in the Math class that can do this. For example, to do the calculation above, you would do something like so:

Code:
result = (int)(2 * Math.pow((double)orbit, 2.0));
 
Back
Top Bottom