Logical operator out of commission?

Asked

Viewed 38 times

-1

Hello I’m doing a project in java that involves stacks and queues, however so far so good, but among some conditional that I did with if one of them "presented" an error (In fact do not present or do what it should do is the "error" in question...

Follow the source code below

     try
 {  
    String equa = txtEqua.getText();
    StringTokenizer quebratuto = new StringTokenizer(equa,"+-*/^()",true);
    Pilha p = new Pilha(quebratuto.countTokens());
    Fila f = new Fila(quebratuto.countTokens());
     TabelaDePrecedencias table = new TabelaDePrecedencias();
        while (quebratuto.hasMoreTokens())
        {       
           String token = quebratuto.nextToken();
            char topo = ' ';
            char sequencia = ' ';
            try
            {
                Double nro = Double.parseDouble(token);
                f.guardeUmItem(nro);
            }
            catch(NumberFormatException erro)
            {
                if (token == "(")  //não está entrando neste cara aqui :/
                {
                    p.guardeUmItem(token);
                    continue;
                }

                if(p.vazia())
                {
                    p.guardeUmItem(token);
                    continue;
                }


                    sequencia = token.charAt(0);
                    teste2.setText(sequencia+"Sequencia");
                    topo = (char)p.getUmItem();
                    teste3.setText((char)topo+"Topo");


                Boolean devoDesenpi = table.devoDesempilhar(topo,sequencia);
                if(devoDesenpi==true && sequencia == ')')
                {
                   teste2.setText(devoDesenpi.toString());
                  char salvaItem = (char)p.getUmItem();
                  while (salvaItem != '(')
                  {
                    p.jogueUmItemFora();
                    f.guardeUmItem(salvaItem);
                    salvaItem = ' ';
                  }
                   if(salvaItem == '(' || salvaItem == ')')
                       p.jogueUmItemFora();
                }
            }
       }
 }
 catch(Exception erro)
 {     
     teste2.setText("Ta errado :/");
 }
  • Which error appears? at which point? try to be more explanatory and put everything you have of code

1 answer

2


Comparison of strings in Java is done with the method equals().

if (token.equals("(")) {
   ...
}

Or compare the individual character, which is of numeric type and can be compared with ==, as an example if (token.charAt(0) == '(') {

Browser other questions tagged

You are not signed in. Login or sign up in order to post.