A.sahinovic A.sahinovic - 5 months ago 18
Java Question

Method gerResultPow not working in calculator

I am building my calculator app right now.Everything is working fine, except pow method.

When I click 8+5, and then pow method, I get these:

calculatorat E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.atlant.calculator.calculatorat, PID: 28984
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4780) 
at android.view.View$PerformClick.run(View.java:19866) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5257) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Caused by: java.lang.NumberFormatException: Invalid double: "7+7"
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.initialParse(StringToReal.java:164)
at java.lang.StringToReal.parseDouble(StringToReal.java:282)
at java.lang.Double.parseDouble(Double.java:301)
at com.atlant.calculator.calculatorat.Calculator.getResultPow(Calculator.java:178)
at com.atlant.calculator.calculatorat.Calculator.onClickPow(Calculator.java:205)
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:4780) 
at android.view.View$PerformClick.run(View.java:19866) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5257) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372)


There is no problem about comparing Strings, I know I can compare them with .equals!.Problem is with my 3rd case of getResultPow.

I only get error from 3.rd case in getResultPow (When I try to sum two numbers and then click POW).POW is working fine for 1.st and 2.nd case (number entered and then clicked POW).

Here is my github repo: [https://github.com/adnxy/CalculatorAt][1]

Anybody suggestions?

Bellow is my onClickPow:

public void onClickPow(View v) {
if (display == "") return;
if (!getResultPow()) return;
{
_screen.setText(String.valueOf(result));
}

}


My getResultPow method with three cases: 1.st and 2.nd case are working ,but when I click third I get error.

//1.st case ,all empty, working fine
if (display == "" && currentOperator == "" && result == "") return false;

//2.nd case, entered number+pow, working fine
if (currentOperator == "" && result == "" )
if (display != "") {
result = String.valueOf(Double.parseDouble(display) * Double.parseDouble(display));
}

//3rd. case, two numbers entered and pow clicked, exception
if (result == "")
if (display != "" && currentOperator != "") {
result = String.valueOf(String.valueOf(result)+Double.parseDouble(display) * Double.parseDouble(display));
return true;
}


[1]: https://github.com/adnxy/CalculatorAt

Answer

See the third error. Your're trying to convert to double the String 7+7. Try to write:

Double pow = Double.parseDouble(display) * Double.parseDouble(display);
result = String.valueOf(Double.parseDouble(Double.parseDouble(result)+pow));