0

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

InsaneCat
  • 1,939
  • 5
  • 16
  • 33

4 Answers4

0

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"

ValW
  • 333
  • 1
  • 8
0

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());
}
Rahul Chokshi
  • 660
  • 4
  • 18
0

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

Hamed Karami
  • 340
  • 1
  • 3
  • 28
0

To simplify the debugging here, focus on this line:

Object ans = engine.eval(val);

  1. Print the value of val so we can see the source that is being evaluated.
  2. Wrap that line in a try catch block and print the full stack trace so we can see the full error diagnostic.
Nicholas
  • 15,306
  • 4
  • 35
  • 62