java 当我从运算符中拆分数字时,为什么拆分方法不能正常工作

5hcedyr0  于 2023-01-04  发布在  Java
关注(0)|答案(2)|浏览(129)

Output of the code

public class Model {
private Scanner sc = new Scanner(System.in);

private String expression;
private String exp[];

    public Model()
    {
        expression = sc.nextLine();
        split();
    }
    
    public void split()
    {
        
        //splitting the entered expression to array of operators alone and array of the numbers then create Arraylist to combine the operators and numbers together as if it is a string expression  but as an array
        String num[]= this.expression.split("[/+/*/-]");
        String preop[]= this.expression.split("[0123456789]"); // this will give [empty, operator, operator...] therefore we will create another array to fill in the ops excluding empty
       
        
                    System.out.println("Test: Printing num Array");
                    for(int i = 0; i<num.length;i++)
                        {
                    System.out.print(num[i]+",");
                        }
                    System.out.println("\nTest: Printing preOp Array");
                    for(int i = 0; i<preop.length;i++)
                    {
                    System.out.print(preop[i]+ ",");
                    }
                
        
        
        ArrayList<String> op = new ArrayList<>();//I used arraylist because easier
        for(int i = 1; i<preop.length;i++)
        {
            op.add(preop[i]);
        }
        
                System.out.println("\nTest of the fixed preOp array: " + op);
                
                
        //putting the operands and the operators together in the same array
                
        ArrayList<String> exp = new ArrayList<>();
        //fill the arraylist with numbers then add the operators to it by using number (index of the operator +1 +count)
        for(int i = 0; i <num.length;i++)
        {
            exp.add(num[i]);
        }
        int count = 0;
        for(int i = 0; i <op.size();i++)
        {
            exp.add(i+1+count, op.get(i));
            count++;
        }
        
                System.out.println("Test: " + exp);
    }

问题是,每当用户在表达式中输入两位数时,op数组就会给出空槽[op,op,empty,op]。
当用户输入一位数字时,我预期会出现类似的结果,如图像input with one digit numbers所示

v1l68za4

v1l68za41#

就是因为这个

this.expression.split("[0123456789]");

我们用一个数字来分割,所以43也被分割成两部分,中间有一个空字符串。另外,你不需要命名正则表达式中的所有数字,你可以只做一个范围"[0-9]"。如果你想匹配一个或多个数字,添加一个+。这应该可以工作:

this.expression.split("[0-9]+");
68bkxrlz

68bkxrlz2#

你可以使用正则表达式在任何一个操作符处拆分,然后使用lookahead和lookbehind来保持它们,从而一次性地解决这个问题:

public static void main(String[] args) {
    String first  = "3+2*3-4";
    String second = "3+2-43*3";

    System.out.println(Arrays.toString(splitExpression(first)));
    System.out.println(Arrays.toString(splitExpression(second)));
}

static String[] splitExpression(String input){
    String regex = "((?<=(\\+|\\-|\\*|\\/))|(?=(\\+|\\-|\\*|\\/)))";
    return input.split(regex);
}

输出:

[3, +, 2, *, 3, -, 4]
[3, +, 2, -, 43, *, 3]

相关问题