-1
Scanner sc = new Scanner(System.in);
       String s = sc.nextLine().trim();
       s= s.replaceAll("\\++"," + ");
       Pattern p = Pattern.compile("[a-zA-Z]+|\\d+|[\\(\\*\\)/%]");
       Matcher m = p.matcher(s);
       while(m.find()){
           String str=m.group();
           s= s.replace(m.group()," "+str+" ").trim();
       }
      String[] str=s.split(" +");
      for(String s1:str) System.out.print(s1+",");

if s=22*(7-9 output is: 22,*,(,7,-,9,

if s=12*(4-2 output is:1,2,*,(,4,-,2,

Why digits of 12 got separated in second example? Help me if someone knows about it

VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • It is working for me. Please see [**this**](https://onlinegdb.com/SyxxG7G6U) –  Jun 13 '20 at 09:53

1 Answers1

0

It happens because there are two 2 in the last expression. When you get the last 2, you add spaces around all 2s, so it leads to 12 to be split.