2

I am a beginner in java , I was making a simple calculator app in Android Studio using Beanshell . When I run the project , It works fine only on my emulator . For real devices , this app can be installed only . When I try to run , the app crashes showing "Unfortunately 'X' app has been stopped.

My code is too simple :

Interpreter interpreter = new Interpreter();
String equation = "2+3*9"; // I gave user to input his own via editText, 
                           // Lets think for a simple case
Object answer1 = interpreter.eval(equation);
String answer = answer1.toString();

Log :

EVENT LOG : http://pastebin.com/JwcuV0ch

GRADLE BUILD MESSAGE : http://pastebin.com/8cDXp0N5

1)Would you please explain why this error occurs?

2)From build message, I am pasting some lines:

Error:(bsh.Interpreter$1) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably    produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler

I got the line compiler that did not target the mordern .class file format repeatedly in my build message . So please let me know how can I recompile BeanShell ?

[ P.S. : In Netbeans I have wrote the same code to evalute value from a string .

import bsh.Interpreter;
public class Main
{
    public static void main(String[] args)
    {
             Interpreter interpreter = new Interpreter();
             String equation = "2+3*9"; // I gave user to input his own via editText in android, 
                           // Lets think for a simple case
              String answer = interpreter.eval(equation);
              System.out.println(answer);
    }
}

Everything was fine there , Now it is strange to me why the same code create problem in Android Studio ]

Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
000
  • 405
  • 5
  • 20

1 Answers1

1

Interpreter.eval() returns an Object, not a String. So you most likely encounter a problem in casting from an Integer to a String:

Target exception: java.lang.ClassCastException: Cannot cast java.lang.Integer to java.lang.String

Try to cast the Integer return value to a String and it should work:

String answer = String.valueOf(interpreter.eval(equation)); 
joschi70
  • 56
  • 2