Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Tokenize the string

I try to tokenize an expression like ( 3 < 5 ) or ( 5 > 10 )

I wrote the code, but it can not see the comparison operators and so can not compare integers. Can you please advise to how to fix this?

The result should be true or false.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public class StackMain {
    public static void main(String[] args) {
        String FileData = "test";
        FileReader readFile = null;
        try {
            readFile = new FileReader(FileData);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        BufferedReader reader = new BufferedReader(readFile);
        String inputFile;
        try {
            inputFile = reader.readLine();
            while (inputFile != null) {
                processInputData(inputFile);
                inputFile = reader.readLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void processInputData(String str) {
        ArrayStack<Integer> intNum = new ArrayStack<>();
        ArrayStack<String> operVal = new ArrayStack<>();

        String[] subStr = str.split(" ");
        for (int i = 0; i < subStr.length; i++) {
            if (subStr[i].charAt(0) >= '0' && subStr[i].charAt(0) <= '9') {
                intNum.push(Integer.parseInt(subStr[i]));
            } else if (subStr[i] == "<" || subStr[i] == ">" || subStr[i] == "==" || subStr[i] == "||"
                    || subStr[i] == "&&") {
                operVal.push(subStr[i]);
            } else if (subStr[i] == ")") {
                int A = intNum.pop();
                int B = intNum.pop();
                String oper = operVal.pop();
                if (oper == "<") {
                    intNum.push(A < B ? 1 : 0);
                } else if (oper == ">") {
                    intNum.push(A > B ? 1 : 0);
                } else if (oper == "==") {
                    intNum.push(A == B ? 1 : 0);
                } else if (oper == "||") {
                    intNum.push(A | B);
                } else if (oper == "&&") {
                    intNum.push(A & B);
                }
            }
        }
        System.out.println("The value of " + str + "is " + (intNum.pop() == 1));
    }
}

>Solution :

You need to pop B, before you pop A.

int B = intNum.pop();
int A = intNum.pop();

Since it is a stack, B was added last, so it needs to be popped off first.

Also, don’t forget to call close() on any readers you may have initialized.

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

public class StackMain {
    public static void main(String[] args) {
        String filename = "test";
        FileReader fileReader = null;
        try {
            fileReader = new FileReader(filename);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String inputFile;
        try {
            inputFile = bufferedReader.readLine();
            while (inputFile != null) {
                processInputData(inputFile);
                inputFile = bufferedReader.readLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fileReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void processInputData(String str) {
        Deque<Integer> intNum = new ArrayDeque<>();
        Deque<String> operVal = new ArrayDeque<>();
        String[] tokens = str.split(" ");
        for (int i = 0; i < tokens.length; i++) {
            switch (tokens[i]) {
                case "0":
                case "1":
                case "2":
                case "3":
                case "4":
                case "5":
                case "6":
                case "7":
                case "8":
                case "9":
                    intNum.push(Integer.parseInt(tokens[i]));
                    break;
                case "<":
                case ">":
                case "==":
                case "||":
                case "&&":
                    operVal.push(tokens[i]);
                    break;
                case ")":
                    int B = intNum.pop();
                    int A = intNum.pop();
                    String oper = operVal.pop();
                    switch (oper) {
                        case "<":
                            intNum.push(A < B ? 1 : 0);
                            break;
                        case ">":
                            intNum.push(A > B ? 1 : 0);
                            break;
                        case "==":
                            intNum.push(A == B ? 1 : 0);
                            break;
                        case "||":
                            intNum.push((A | B) == 1 ? 1 : 0);
                            break;
                        case "&&":
                            intNum.push((A & B) == 1 ? 1 : 0);
                            break;
                    }
                    break;
            }
        }
        System.out.printf("The value of %s is %b%n", str, (intNum.pop() == 1));
    }
}

Input (filename: test)

( 3 < 5 )

Output

The value of ( 3 < 5 ) is true
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading