Java-后缀表达式中的不规则空格

Java-后缀表达式中的不规则空格,java,postfix-notation,infix-notation,Java,Postfix Notation,Infix Notation,我的程序运行得很好,但输出中有一些不规则的空格。例如,如果输入为44*5+6,则输出为445*6+。我试图修改添加到字符串后缀的所有代码行,但没有效果。我希望输出的形式是:操作数运算符,即操作数和运算符之间的1个空格 这是我的密码: import java.util.*; public class PostfixConversion { public static void main(String args[]) { System.out.print("Enter

我的程序运行得很好,但输出中有一些不规则的空格。例如,如果输入为44*5+6,则输出为445*6+。我试图修改添加到字符串后缀的所有代码行,但没有效果。我希望输出的形式是:操作数运算符,即操作数和运算符之间的1个空格

这是我的密码:

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();

        String postfix = convertToPostfix(infix);

        System.out.println(postfix);

        //System.out.println("The result of calculation is: " + postfixEvaluate("23+"));

    }

 //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 == '+')
             {
                 //postFix += " ";

                 while(!stack.empty() && precedence(stack.peek(), character))
                     postFix += stack.pop();

                 stack.push(character);
             }  else if(character == '(') {
                 stack.push(character);
             }  else if(character == ')') {
                 while(!stack.peek().equals('(') && !stack.isEmpty())
                     postFix += stack.pop();

                 if(!stack.isEmpty() && stack.peek().equals('('))
                     stack.pop(); // pop/remove left parenthesis
             }  else
                 postFix += character;
         }
         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;
    }

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

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

         if(v1 < v2)
            return false;

         return true;
    }

首先从输入中删除所有空白,以便它们不会破坏您的格式:infixExp=infixExp.replaceAll\\s,;然后在需要的地方添加空格。

不仅仅是不规则的空格。它只是不能正确处理空间。e、 g如果我通过5+2,但没有任何空格,则out不会添加任何空格。这会给你一个线索,你可以用调试程序来验证是的,这是作业的要求之一。我们的教授告诉我们在操作数和运算符之间添加一个空格,以使我们的生活更轻松。因此,请遵循指导原则。我要说的是,空间添加不正确。如果我不在操作数之间指定空格,则您的程序不会添加它: