为什么此方法返回false?如何覆盖.equals()?

时间:2019-05-31 13:00:57

标签: java stack

我必须输出开放括号的最大数量,因此我实现了一个Stack,在其中插入所有开放括号,并为该属性提供了一个匹配方法。例如,如果在“(”之后有“)”我必须.pop()“(”。我现在的问题是,我从文本中得到了两个数组,其中一个数组我有开括号,第二个数组中有闭括号。

我的顺序符合我的意思。为了将括号推入堆栈中,我曾考虑使用“ for”来读取序列,当元素是一个开放的括号时,我会将其推入堆栈中。我用Array.asList进行了检查,因此当括号被打开并且打开括号的数组被压入堆栈后就包含了它。对于封闭的括号,我执行相同的操作,但是当关闭时,我必须检查括号是否首先打开,我实现了“ matching”方法。 我认为方法“ matching”返回false,如果我使用.equals(),怎么可能是覆盖问题?我该如何覆盖?

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

public class Esercizio1t
{
    public static boolean matching(String aperta, String chiusa)
    {
        if (aperta.equals("(") && chiusa.equals(")"))
        {
            return true;
        }
        if (aperta.equals("[") && chiusa.equals("]"))
        {
            return true;
        }
        if (aperta.equals("`") && chiusa.equals("\'"))
        {
            return true;
        }
        if (aperta.equals("<") && chiusa.equals(">"))
        {
            return true;
        }
        if (aperta.equals("$") && chiusa.equals("&"))
        {
            return true;
        }
        if (aperta.equals("\\") && chiusa.equals("/"))
        {
            return true;
        }
        if (aperta.equals("?") && chiusa.equals("!"))
        {
            return true;
        }
        if (aperta.equals("{") && chiusa.equals("}"))
        {
            return true;
        }
        else
            return false;
    }


    public static void main(String[] args) throws IOException
    {

        //String fileinput=args[0];
        //String fileoutput=args[1];

        Scanner br = new Scanner(new FileReader("Input1Es1.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("Outputtest.txt"));

        String s = br.nextLine();
        int dimensione = Integer.parseInt(s);
        String s2 = br.nextLine();
        String[] array = s2.split("");
        String[] v = new String[dimensione];
        String[] m = new String[dimensione];


        for (int i = 0; i < array.length; i++)
        {
            if (i < dimensione)
            {
                v[i] = array[i];
            }
            else if (i > dimensione)
            {
                for (int j = 0; j < array.length / i; j++)
                {
                    m[j] = array[i];
                }
            }
        }

        String s3 = br.nextLine();
        String[] c = new String[s3.length()];
        c = s3.split("");
        Stack<String> Pila = new Stack<String>();

        int cont = 0;
        int posizione = 0;
        int contenitore = 0;
        for (int i = 0; i < c.length; i++)
        {
            if (Arrays.asList(v).contains(c[i]))
            {
                Pila.push(c[i]);
                System.out.println(c[i]);
                cont++;
            }
            else if (Arrays.asList(m).contains(c[i]))
            {
                if (matching(Pila.peek(), c[i]))
                {
                    posizione++;
                    if (posizione == 1)
                    {
                        contenitore = cont;
                    }
                    Pila.pop();
                }
            }
        }


        if (Pila.empty() || posizione == 1)
        {
            System.out.println(contenitore);
            bw.write(Integer.toString(contenitore));
        }
        else
        {
            System.out.println(-1);
            bw.write(Integer.toString(-1));
        }
        br.close();
        bw.flush();
        bw.close();
    }
}

我有这个输入:2 //不同括号类型的数量 ([]] //打开和关闭括号 ((123([445()23] 4)()[()]))//检查顺序

在这种情况下,输出为:5但我有-1

0 个答案:

没有答案
相关问题