后缀表达式输出中缺少空格 - Java

Missing Spaces in the Postfix expression output - Java

我的代码成功地将中缀表达式转换为后缀表达式。但是,当我输入一个多于 1 位的数字(例如 546)时,我在这个数字和正确的操作数之间没有空格。

我的代码测试 运行: 输入:输入一个表达式:(24/4) / (15/3) * 10 - 4 + 2 输出:后缀表达式为:244/ 153/ / 10 * 4 - 2+

我希望它是 后缀表达式是:24 4/ 15 3/ / 10 * 4 - 2+

这是我的代码:请提出任何允许我在输出中插入空格的更改。

import java.util.*;

public class PostfixConversion {

    public static void main(String args[]) {

        System.out.print("Enter an expression: ");
        String infix = new Scanner(System.in).nextLine();

        System.out.println(convertToPostfix(infix));

    }


   public static boolean precedence(char first, char second)
   {
      int v1 = 0, v2 = 0;
      //find value for first
      if(first == '-' || first == '+'){
         v1 = 1;
      }else if(first == '*' || first == '/'){
         v1 = 2;    
      }//end if

      //find value for second
      if(second == '-' || second == '+'){
         v2 = 1;
      }else if(second == '*' || second == '/'){
         v2 = 2;    
      }//end if

     if(v1 < v2){
        return false;
     }//end if

     return true;
  }//end precedence method

 //converts infix expression into postfix expression
 public static String convertToPostfix(String infixExp)
  {
     String postFix = "The Postfix Expression is: ";
     Stack<Character> stack = new Stack<Character>();
     char character = ' ';

     for(int i = 0; i < infixExp.length(); i++)
     {
         character = infixExp.charAt(i);

         //determine if character is an operator
         if(character == '*' || character == '-' || character == '/' || character == '+')
         {
             while(!stack.empty() && precedence(stack.peek(), character)){
                 postFix += stack.pop();
             }//end while
             stack.push(character);
         }
         else if(character == '(') //check for left parenthesis
         {
             stack.push(character);
         }
         else if (character == ')') 
         {
             while(!stack.peek().equals('(') && !stack.isEmpty()){ //add characters until left parenthesis
                 postFix += stack.pop();
             }//end while

             if(!stack.isEmpty() && stack.peek().equals('(')){
                 stack.pop(); // pop/remove left parenthesis
             }
         }
         else
         {
             postFix += character;
         }//end if
     }//end for
     while(!stack.empty()) //add the remaining elements of stack to postfix expression
     {
         if(stack.peek().equals('('))
         {
             postFix = "There is no matching right parenthesis.";
             return postFix;
         }
         postFix += stack.pop();
     }
         return postFix;
 }//end convertToPo
}

修复方法是在此处添加一行(请参阅代码注释):

if(character == '*' || character == '-' || character == '/' || character == '+')
{
    postFix += ' '; // <-- add space here
    while(!stack.empty() && precedence(stack.peek(), character)){
        postFix += stack.pop();
    }//end while
    stack.push(character);
}

应用修复程序后,我机器上的输出如下所示:

The Postfix Expression is: 24 4/   15 3/  / 10  * 4  - 2+