I am unable to evaluate string using eval()

I am unable to evaluate string using eval()



I am trying to make a calculator application.I want to evaluate the contents of a TextView and display it as a toast message. The eval() statement throws an exception for ScriptException as below.


"Unhandled exception: javax.script.ScriptException"



Even when I have imported javax.script.ScriptException. I am not getting from why it is throwing exception.



Here is my xml code:


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginTop="95dp"
android:textAlignment="center"
android:textSize="36sp" />

<Button
android:id="@+id/n1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="18dp"
android:layout_marginTop="194dp"
android:text="1" />

<Button
android:id="@+id/n2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/n1"
android:layout_centerHorizontal="true"
android:text="2" />

<Button
android:id="@+id/n3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignTop="@+id/n1"
android:layout_marginEnd="16dp"
android:text="3" />

<Button
android:id="@+id/n4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/n1"
android:layout_centerVertical="true"
android:text="4" />

<Button
android:id="@+id/n5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="5" />

<Button
android:id="@+id/n6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/n3"
android:layout_centerVertical="true"
android:text="6" />

<Button
android:id="@+id/n7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignStart="@+id/n1"
android:layout_marginBottom="194dp"
android:text="7" />

<Button
android:id="@+id/n8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/n7"
android:layout_centerHorizontal="true"
android:text="8" />

<Button
android:id="@+id/n9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/n3"
android:layout_alignTop="@+id/n7"
android:text="9" />

<Button
android:id="@+id/n0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="129dp"
android:text="0" />

<Button
android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/n3"
android:layout_alignTop="@+id/n0"
android:text="CLEAR" />

<Button
android:id="@+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="73dp"
android:text="+" />

<Button
android:id="@+id/equal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/n3"
android:layout_alignTop="@+id/plus"
android:text="=" />

</RelativeLayout>

</android.support.constraint.ConstraintLayout>



and here is my java code:


package com.example.poopypigeon.calx;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class MainActivity extends AppCompatActivity
Button n1;
Button n2;
Button n3;
Button n4;
Button n5;
Button n6;
Button n7;
Button n8;
Button n9;
Button n0;
Button clear;
Button plus;
Button equal;
TextView value;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
value = findViewById(R.id.textView);
n1 = findViewById(R.id.n1);
n1.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String val = value.getText().toString();
value.setText(val+"1");

);
n2 = findViewById(R.id.n2);
n2.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String val = value.getText().toString();
value.setText(val+"2");

);
n3 = findViewById(R.id.n3);
n3.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String val = value.getText().toString();
value.setText(val+"3");

);
n4 = findViewById(R.id.n4);
n4.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String val = value.getText().toString();
value.setText(val+"4");

);
n5 = findViewById(R.id.n5);
n5.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String val = value.getText().toString();
value.setText(val+"5");

);
n6 = findViewById(R.id.n6);
n6.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String val = value.getText().toString();
value.setText(val+"6");

);
n7 = findViewById(R.id.n7);
n7.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String val = value.getText().toString();
value.setText(val+"7");

);
n8 = findViewById(R.id.n8);
n8.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String val = value.getText().toString();
value.setText(val+"8");

);
n9 = findViewById(R.id.n9);
n9.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String val = value.getText().toString();
value.setText(val+"9");

);
n0 = findViewById(R.id.n0);
n0.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String val = value.getText().toString();
value.setText(val+"0");

);
clear = findViewById(R.id.clear);
clear.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
value.setText("");

);
plus = findViewById(R.id.plus);
plus.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String val = value.getText().toString();
value.setText(val + "+");

);
equal = findViewById(R.id.equal);
equal.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String val = value.getText().toString();
ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js");
Object ans = engine.eval(val);
Toast toast = Toast.makeText(MainActivity.this,"the ans is: "+ ans,Toast.LENGTH_SHORT);
toast.show();

);




PS. i need a reply as soon as possible as this is for a school project




4 Answers
4



Change ScriptException with Exception


try
ans = engine.eval(val);
catch (Exception e)
e.printStackTrace();



You are not sure for which type of exception it may trigger so it will display the same message for all the exceptions and user may not be able to understand which exception occurred. Thats the reason you should place is at the end of all the specific exception catch blocks



I would do it like this to further investigate:


try{
ScriptEngineManager manager = new ScriptEngineManager()
ScriptEngine engine = manager.getEngineByExtension("js");
Object ans = engine.eval(val);
Toast toast = Toast.makeText(MainActivity.this,"the ans is: "+ ans,Toast.LENGTH_SHORT);
toast.show();
catch(ScriptEngineManager e)
// handle exception
System.err.println("Error making calculation: " + e.getMessage());



EDIT: Change:
String val = value.getText().toString();
value.setText(val+"1");
You have only the value in the String val - but you need the content of value textfield. In the String val there has to be something like "4+4".



You just add numbers to the textfield but no operators.



value.setText(val+"1"); -> You just add the Number 1 to the Textfield, so after clicking some Buttons you have like this in your Textfield "512312"





ok i will try it
– sahil bhatnagar
Aug 28 at 9:04





and check whats in 'val' before making the .eval statement.
– ValW
Aug 28 at 9:04





i am still getting the same exception
– sahil bhatnagar
Aug 28 at 9:10





I do have a button with a plus operator. I will add the others later
– sahil bhatnagar
Aug 29 at 10:50






yes like @Nicholas pointed out - check whats in 'val' before doing the 'Object ans = engine.eval(val);'
– ValW
Aug 29 at 11:33



Copy this import statement


import javax.script.ScriptException;



Try is code:


try
Object ans = engine.eval(val);
System.out.println("It worked!:" + ans.toString());

catch (ScriptException se)
System.out.println("Problem in eval:", se.getmessage());





it isn't working
– sahil bhatnagar
Aug 28 at 9:14





copy this import statement:- import javax.script.ScriptException;
– Rahul Chokshi
Aug 28 at 9:22





i have already done that
– sahil bhatnagar
Aug 28 at 9:45



To simplify the debugging here, focus on this line:



Object ans = engine.eval(val);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)