unable to correctly validate balanced parenthesis parsing in java method

前端 未结 2 949
-上瘾入骨i
-上瘾入骨i 2021-01-29 01:47

I have a method that\'s supposed to validate accurate opening and closing parenthesis in a string using java. This method will be used to parse mathematical expressions so it\'s

2条回答
  •  执念已碎
    2021-01-29 02:34

    The immediate problem is this

    String[] tokens = str.split("");
    

    Gives you first char = "" if you use java 1.7 or less, so you will exit your loop since stack is empty...

    Note: this has been changed in java 1.8 split difference between java 1.7 and 1.8

    change to:

    char[] tokens = str.toCharArray();
    

    I guess however that you need to consider the fact that there can be chars before your first ( and that you may have other chars then ( and )

提交回复
热议问题