Why didn’t he split?

I am confused about why it is not breaking a string? my exp string array does not contain anything when I debug that this split is wrong, what I'm trying to do is break up a very simple expression like 1 + 2 + 3 and then parse the values ​​by making a calculator.

EDIT Hello, why am I splitting into each character, because I am doing a calculator and reading something about converting an infix to a postfix, so I need to split the line and then skip each line and perform the check as shown below, however, when I debug , it shows that exp [] is empty

For each token in turn in the input infix expression:

    * If the token is an operand, append it to the postfix output.
    * If the token is an operator A then:
          o While there is an operator B of higher or equal precidence than A at the top of the stack, pop B off the stack and append it to the output.
          o Push A onto the stack.
    * If the token is an opening bracket, then push it onto the stack.
    * If the token is a closing bracket:
          o Pop operators off the stack and append them to the output, until the operator at the top of the stack is a opening bracket.
          o Pop the opening bracket off the stack.

When all the tokens have been read:

    * While there are still operator tokens in the stack:
          o Pop the operator on the top of the stack, and append it to the output.


   // the main class
public class Main {


    public static void main(String[] args) {
       calcExpChecker calc = new calcExpChecker("1+2+3+4");
       calc.legitExp();
       calc.displayPostfix();
    }

}
//the class
package javaapplication4;
import java.util.*;


public class calcExpChecker {


    private String originalExp; // the orginal display passed
    private boolean isItLegitExp; // the whole expression is it legit
    private boolean isItBlank; // is the display blank?
    private StringBuilder expression = new StringBuilder(50);
    private Stack stack = new Stack();//stack for making a postfix string

    calcExpChecker(String original)
    {
        originalExp = original;
    }

     //check for blank expression
    public void isitBlank()
    {
        if(originalExp.equals(""))
        {
            isItBlank = true;
        }
        else
        {
            isItBlank = false;
        }

    }

    //check for extra operators
    public void legitExp()
    {
      String[] exp = originalExp.split(".");
      for(int i = 0 ; i < exp.length ; i++)
      {
          if(exp[i].matches("[0-9]"))
          {
              expression.append(exp[i]);
          }
          else if(exp[i].matches("[+]"))
          {
             if(stack.empty())
             {
                 stack.push(exp[i]);
             }
             else
             {
                 while(stack.peek().equals("+"))
                 {
                    expression.append(stack.pop());
                 }
                 stack.push(exp[i]);
             }
          }
        if (!stack.empty())
        {
            expression.append(stack.pop());
        }
      }

    }

    public void displayPostfix()
    {
        System.out.print(expression.toString());
    }
}
+3
source share
4 answers

If you make each character a separator, what's in between? Nothing

., 1 + 2 + 3 + 4 1 ? , , . ?+. . ? 2. etc etc

+3

, string.split("").

for (String part : string.split("")) {
    // ...
}

, , string.toCharArray().

for (char c : string.toCharArray()) {
    // ...
}

switch, , if/else.

+2

foreach String. , exp [i].

""."

0

:

, , , Java # . , , - , #!

Btw, #, RegEx "." , (""), .

Edit

split():

string expressions = "10+20*4/2";

/* this will separate the string into an array of numbers and the operators;
   the numbers will be together rather than split into individual characters
   as "" or "." would do;
   this should make processing the expression easier
   gives you: {"10", "+", "20", "*", "4", "/", "2"} */
foreach (string exp in expressions.split(@"(\u002A)|(\u002B)|(\u002D)|(\u002F)"))
{
  //process each number or operator from the array in this loop
}

String[] exp = originalExp.split(".");

split() ( ). , , , .

-1

Source: https://habr.com/ru/post/1756787/


All Articles