jeudi 3 janvier 2019

Can someone please tell me why this code doesn't go inside the if or the else if statments although the types are the same? [duplicate]

This question already has an answer here:

Arithmetic expression file is like this: (2+5)

(5*(3+2)

((314+542)+(3*14))

(9+*2)

((2)

((3*33)+333)

(4*(4*(4*4)))

I am just trying to see if an expression is valid or not. I know that there are more problems to address rather than if the parenthesis are the same number or not but I just started with that and it never seems to go inside the if or the else if.

import java.io.*;
import java.util.*;

public class q3ff {
static Stack<String> operands;
static Stack<String> operators;
static int count_left_par;
static int count_right_par;

public static void main(String args[]) {
    read_file();
}

public static void read_file(){
    operands = new Stack<String>();
    operators = new Stack<String>();
    int left_count_par = 0;
    int right_count_par = 0;

    try {
        BufferedReader reader = new BufferedReader(new FileReader("arithmetic_expression.txt"));
        String line;
        while((line = reader.readLine()) != null) {
            String[] splitted_line = line.split("(?!^)");
            for(String par : splitted_line){
                System.out.println(par.getClass().getName());
                if(par ==")"){
                    operands.push(par);
                    left_count_par++;
                    System.out.println("done2");
                }
                else if(par == "(") {
                    operands.push(par);
                    right_count_par++;
                    System.out.println("done1");
                }

                else {
                    operators.push(par);
                    System.out.println("done0");
                }
            }
            splitted_line = null;               
            line = reader.readLine();

        }
    }
    catch(Exception e) {
        System.out.println("This either does not exist or you messed up.");
    }

}
}

Aucun commentaire:

Enregistrer un commentaire