Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why did it not split?

Tags:

java

i am confused on why it doest not split the string? my array of string exp does not contain anything when i debug it is the split wrong?what i am trying to do is to split a very simple expression like 1+2+3 and then parse the values, doing a calculator.

EDIT hi, why i am splitting on each character is because i am doing a calculator, and have read something about converting infix to postfix,so i need to split the string and then loop through each of the string and do the checking as shown below,however when i debug it shows the 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());
    }
}
like image 853
sutoL Avatar asked Sep 17 '26 18:09

sutoL


2 Answers

If you make every character a delimiter, what is between them? Nothing

e.g., 1+2+3+4 is 1 a delimiter? yes, ok, capture everything between it and the next delimiter. Next delimiter? +. Nothing captured. Next delimiter? 2. etc etc

like image 189
Affe Avatar answered Sep 19 '26 06:09

Affe


You want to split on every character, so rather use string.split("").

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

Or better, just iterate over every character returned by string.toCharArray().

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

With chars you can use a switch statement which is better than a large if/else block.

like image 25
BalusC Avatar answered Sep 19 '26 07:09

BalusC