Java 从字符串中提取数字和运算符

Java 从字符串中提取数字和运算符,java,string,numbers,expression,extract,Java,String,Numbers,Expression,Extract,这是我写的代码: public static boolean isOperator(char op){ if (op == '+' || op == '-' || op == '*' || op == '/' || op == '^' || op == '(' || op == ')'){ return true; } return false; } public static bo

这是我写的代码:

public static boolean isOperator(char op){
    if (op == '+' || op == '-'
            || op == '*' || op == '/'
            || op == '^'
            || op == '(' || op == ')'){
        return true;
    }
    return false;
}

public static boolean isOperand(char op){
    String numbers = "0123456789.";
    int a = numbers.indexOf(op);
    return a >= 0;
}
public static void main(String []args){        
    String exp= "15+20+(3.84*25)*(78/3.8)";
    LinkedList a = new LinkedList();

    for (int i = 0; i < exp.length(); i++){
        if (isOperator(exp.charAt(i))){
            a.add(exp.charAt(i));
        } else if (isOperand(exp.charAt(i))){
            int k = i;
            while (k < exp.length()){//I see the length of the number
                if (isOperand(exp.charAt(k))){
                    k++;
                } else {
                    break;
                }
            }
            if (k != exp.length()-1){//if it's not ad the end
                a.add(exp.substring(i, k));
            } else {//if it's at the end I take all the remaining chars of the string
                a.add(exp.substring(i));
            }
            i = k-1;//i must go back since the subtring second parameter is exclusive
        } 
    }
    System.out.println(a);    
}//main
这正是我想要的。如您所见,我将操作数和运算符分别放入一个列表中,以保持字符串的顺序。 有一种更简单的方法吗

有没有更简单的方法

是的,有。使用正则表达式。看一下下面的代码和代码 试着为你的输入运行它

public static void main(String[] args) 
{
    String exp = "15+20+(3.84*25)*(78/3.8)";
    String regex = "(\\d+\\.\\d+)|(\\d+)|([+-/*///^])|([/(/)])";

    Matcher m = Pattern.compile(regex).matcher(exp);

    LinkedList list = new LinkedList();

    while (m.find()) {
        list.add(m.group());
    }

    System.out.println(list);
}
上面使用的我的正则表达式的解释:

“(\d++.\d+)|(\d+)|([+-/*/^])|([/(/)])”


(double)或(integer)或(算术运算符)或(left/right paradensis)

执行此类解析工作的最佳方法是使用解析器生成器。然而,如果你想自己做,你有很多选择。在这种情况下,您也可以这样做:

  public static void main(String[] args) throws Exception{ 
      String exp = "15+20+(3.84*25)*(78/3.8)";
      LinkedList<String> a = new LinkedList<String>();

      StringTokenizer st = new StringTokenizer(exp, "+*/-()", true);
      while(st.hasMoreTokens())
          a.add(st.nextToken());
    } 
publicstaticvoidmain(字符串[]args)引发异常{
字符串exp=“15+20+(3.84*25)*(78/3.8)”;
LinkedList a=新建LinkedList();
StringTokenizer st=新的StringTokenizer(exp,“+*/-()”,true);
而(st.hasMoreTokens())
a、 添加(st.nextToken());
} 

这看起来像是一个问题。@AR89请检查我的解决方案,我希望它是您要查找的解决方案。我在正则表达式中添加了“^”运算符:String regex=“(\\d+\\.\\d+)”(\\d+)([+-/*/^])([/(/)”;来自:StringTokenizer是一个遗留类,出于兼容性原因保留它,尽管新代码中不鼓励使用它。建议任何寻求此功能的人改用String的split方法或java.util.regex包。
  public static void main(String[] args) throws Exception{ 
      String exp = "15+20+(3.84*25)*(78/3.8)";
      LinkedList<String> a = new LinkedList<String>();

      StringTokenizer st = new StringTokenizer(exp, "+*/-()", true);
      while(st.hasMoreTokens())
          a.add(st.nextToken());
    }