本文介绍了无法正确验证的Java方法平衡括号解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个是应该来验证用java字符串准确的开幕式和闭幕式括号的方法。该方法将用于解析数学EX pressions所以这是非常重要的括号平衡。出于某种原因,它返回false在这两个运行的:

 的System.out.println(parChecker((())); //返回false :-)
的System.out.println(parChecker(((())))); ?? //返回false :-(为什么
 

下面是一种使用堆栈来解决这个问题的方法。有些事情错在这里监守它返回false为一套平衡括号的为好。什么问题?谢谢你在前进。

 公共静态布尔parChecker(字符串str){
    的String []标记= str.split();
    INT大小= tokens.length;
    堆栈theStack =新的堆栈(大小);
    INT索引= 0;
    布尔平衡= TRUE;
    而((指数<大小)及和放大器;平衡){
        串符号=令牌[指数]
        如果(symbol.equals(()){
            theStack.push(符号);
        } 其他 {
            如果(theStack.isEmpty()){
                平衡= FALSE;
            } 其他 {
                theStack.pop();
            }
        }
        指数++;
    }

    如果(平衡&安培;&安培; theStack.isEmpty()){
        返回true;
    } 其他 {
        返回false;
    }

}
 

下面是我用我的Stack类:

 公共类堆栈{

    私有对象[]栈;
    私人诠释MAXSIZE = 0;
    私人诠释顶部;

    公共堆栈(INT尺寸){
        MAXSIZE =大小;
        堆栈=新的对象[MAXSIZE];
        顶= -1;
    }

    公共无效的push(obj对象){
        顶级++;
        堆叠[顶] = OBJ;
    }

    公共对象POP(){
        返回堆栈[top--]
    }

    公共对象偷看(){
        返回堆栈[顶]
    }

    公共布尔的isEmpty(){
        返回(上== -1);
    }

    公共布尔isFull(){
        返程(上== MAXSIZE -1);
    }
}
 

解决方案

最直接的问题是这样的

 的String []标记= str.split();
 

为您提供了第一个字符=如果你使用的Java 1.7 或更少,所以你会退出你的循环,因为堆栈是空的......

请注意:这已经在的Java 1.8 split java的1.7和1.8 差异

更改为:

 的char []标记= str.toCharArray();
 

我想不过,你需要考虑的是,有可能是在你的第一个字符,您可能有其他字符,然后

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 important for the parenthesis to be balanced. For some reason, it is returning false in both of these runs:

System.out.println(parChecker("(()"));  // returns false :-)
System.out.println(parChecker("((()))")); // returns false :-(  WHY??

Here is the method that uses a stack to solve the problem. Something is wrong here becuase it's returning false for a balanced set of parenthesis as well. What's the problem? Thank you in advance.

public static boolean parChecker(String str) {
    String[] tokens = str.split("");
    int size = tokens.length;
    Stack theStack = new Stack(size);
    int index = 0;
    boolean balanced = true;
    while ((index < size) && balanced) {
        String symbol = tokens[index];
        if (symbol.equals("(")) {
            theStack.push(symbol);
        } else {
            if (theStack.isEmpty()) {
                balanced = false;
            } else {
                theStack.pop();
            }
        }
        index++;
    }

    if (balanced && theStack.isEmpty()) {
        return true;
    } else {
        return false;
    }

}

Here is my stack class that I'm using:

public class Stack {

    private Object [] stack;
    private int maxSize = 0;
    private int top;

    public Stack(int size){
        maxSize = size;
        stack = new Object[maxSize];
        top = -1;
    }

    public void push(Object obj){
        top++;
        stack[top] = obj;
    }

    public Object pop(){
        return stack[top--];
    }

    public Object peek(){
        return stack[top];
    }

    public boolean isEmpty(){
        return (top == -1);
    }

    public boolean isFull(){
        return (top == maxSize -1);
    }
}
解决方案

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 )

这篇关于无法正确验证的Java方法平衡括号解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:10
查看更多