Wednesday, 13 December 2017

PROGRAM TO VALIDATE THE EXPRESSION WITH BINARY OPERATOR.


Program:

package symbol_table;
import java.util.Arrays;
import java.util.Scanner;

public class Bnry_operator
{   
    String result[] = new String[50];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String myString = sc.nextLine();
        String[] result = myString.split("(?<=[-+*/])|(?=[-+*/])");
        String[] as = {"+", "-", "/", "*"};
        System.out.println(Arrays.toString(result));
        int flag = 0;
        System.out.println(result.length);
        for (int i = 0; i < result.length ; i++) {
            if (result[i].toString().contains("+")) {
                flag++;
            } else if (result[i].toString().contains("-")) {
                flag++;
            } else if (result[i].toString().contains("/")) {
                flag++;
            } else if (result[i].toString().contains("*")) {
                flag++;
            } else if (result[i].toString().contains("%")) {
                flag++;
            } else {
            }
        }
        int pp=(result.length-flag)+1;
        if (flag == ((result.length) - flag) - 1) {
            System.out.println("Valid Expression ");
        } else {
            System.out.println("Invalid Expression ");
        }
    }

}


OUTPUT:






No comments:

Post a Comment