0

I'm currently working on a school project in Android Studio and so far I've written (with a little help from stackoverflow.com) an algorithm which generates a random equation everytime you press a button on the screen. The equation then shows up in a textview on the screen. I've added a second textview in which the result of the equation shows up. Here is my code so far:

String[] operationSet = new String[]{"+", "-", "/", "*"};

public void generate(View view) {
    Random random = new Random();
    int numOfOperations = random.nextInt(2) + 1;

    List<String> operations = new ArrayList<>();

    for (int i = 0; i < numOfOperations; i++) {
        String operation = operationSet[random.nextInt(4)];
        operations.add(operation);
    }

    int numOfNumbers = numOfOperations + 1;
    List<Integer> numbers = new ArrayList<>();

    for (int i = 0; i < numOfNumbers; i++) {
        int number = random.nextInt(10)+1;
        numbers.add(number);
    }

    String equation = "";
    for (int i = 0; i < numOfOperations; i++) {
        equation += numbers.get(i);
        equation += operations.get(i);
    }
    equation += numbers.get(numbers.size() -1);

    TextView TextEquation = (TextView)findViewById(R.id.textView);
    TextEquation.setText(equation);

    String stringResultOfEquation = String.valueOf(equation);

    // Calculate the result of the equation

    double doubleEquation = Double.parseDouble(equation);
    double doubleResult = abs(doubleEquation);
    String stringResult = String.valueOf(doubleResult);

    TextView textResult = (TextView)findViewById(R.id.textView2);
    textResult.setText(stringResult);

}

The problem now is, that when I launch the app in the emulator and I press the button on the screen, my app crashes and giving the error message: "unfortunately SolveIt has stopped" Everything worked perfectly fine until I've added the second textview2 with the result of the equation in it. So I guess there must be something wrong with it:

double doubleEquation = Double.parseDouble(equation);
double doubleResult = abs(doubleEquation);
String stringResult = String.valueOf(doubleResult);

TextView textResult = (TextView)findViewById(R.id.textView2);
textResult.setText(stringResult);

Maybe there is a problem with converting my string into a double but I really don't know.

Here is the logcat if someone is wondering:

10-12 22:41:23.646 14905-14905/mycompany.solveit E/AndroidRuntime: FATAL EXCEPTION: main
                                                               Process: mycompany.solveit, PID: 14905
                                                               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:5254)
                                                                   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:5254) 
                                                                   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: "3/1"
                                                                   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 mycompany.solveit.FullscreenActivity.generate(FullscreenActivity.java:210)
                                                                   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:5254) 
                                                                   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) 

10-12 22:41:25.672 14905-14905/mycompany.solveit I/Process: Sending signal. PID: 14905 SIG: 9

If anything is unclear in my question, feel free to ask and I will try to clarify the problem :)

Thank you already in advance!

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
ztv
  • 134
  • 1
  • 9
  • 2
    Please post the logcat. – Andy Turner Oct 14 '16 at 10:15
  • 3
    But I'm going to guess, if it's in the "something wrong with it" code, it's a `NumberFormatException`, caused by `Double.parseDouble(equation);`: this will only parse numbers, not evaluate equations. – Andy Turner Oct 14 '16 at 10:16
  • I would try to catch `NumberFormatException` to test if this is the problem. – MrSmith42 Oct 14 '16 at 10:17
  • @Andy Turner So how should I convert my equation so I can use the "abs" command? Because the "abs" command can't use strings. – ztv Oct 14 '16 at 11:43

0 Answers0