An InputMismatchException is thrown by a Scanner to indicate
“An InputMismatchException is thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type”. For example, if you use the nextDouble to read a token that is not of the type double.
Write a program that prompts the user to enter a mathematical formula (e.g., 4.3 + 5.1) and then displays the result. Your program should prompt the user to try again if the input has an invalid number or invalid operator (i.e., valid operators are: +, -, /, and *). Assume the user enters spaces between the operands and the operator. Read the numbers using the Scanner’s nextDouble method, and the operator using the next method.
Use a while loop that will keep prompting the user for input as long as it is invalid.
Use a try-catch statement to check for the numbers’ validity.
Use a selection statement (e.g., if, switch, etc.) to check for the operator’s validity.
//bold characters are inputs
Sample:
Enter a simple mathematical formula: 4.5 plus 5.3
Invalid Operator. Try again.
4.5a + 5.3
Invalid number format. Try again.
4.5 + 5.3
Result: 9.8
Solution
package chegg;
import java.util.Scanner;
import javax.script.ScriptEngine;
 import javax.script.ScriptEngineManager;
 import javax.script.ScriptException;
public class InputMismatch {
    public static void main(String []args) throws ScriptException
    {
        ScriptEngineManager mgr = new ScriptEngineManager();
        ScriptEngine engine = mgr.getEngineByName(\"JavaScript\");
        Scanner sc=new Scanner(System.in);
        String exp=sc.nextLine();
        if(exp.matches(\"^(?=.*[A-Z])(?=.*[0-9])[A-Z0-9]+$\"))
        {
            System.out.println(\"Invali-1\");
        }
        else
        {
            if(exp.contains(\"[a-zA-Z]\"))
            {
                System.out.println(\"\");
            }
            else
            {
                try{
                    System.out.println(engine.eval(exp));
                }
                catch(Exception im)
                {
                    System.out.println(\"Invalid Expression\");
                }
            }
        }
        //exp.split(\" \");
       
    }
}


