-1

I am trying to input an infix expression and convert it to a postfix expression using Stack class. I will be outputting the postfix expression as a String.

This is my code so far:

import java.util.*;
public class Fix 
{
    public static void main(String[]args)
    {
        Scanner s= new Scanner (System.in);

        System.out.println("Enter expression in infix: ");
        String input= s.nextLine();

        Stack<String> operator= new Stack<String>();
        Stack<String> operand= new Stack<String>();
        System.out.println("Created stacks...");
        String expression="";
        transferToString(input, operator, operand, expression);
        System.out.println("Created string... ");
        System.out.println(expression);

    }
}

Here is the statement that creates the String expression in the void transferToString method:

else if (operand.empty()==false)
            {
                String str1 = input.substring(i++,i+2);
                expression.concat(operand.pop());
                expression.concat(str1);
            }

The above code is a nested if statement, with the primary condition being whether the next character is an operand or not.

In the above else-if statement, I pop an operand from the stack operand, and concatenate it to String expression. In the main method, I print out expression.

However, this is my output:

Enter expression in infix: 
(1+5)
Created stacks...
Pushed first operator!
Created string... 

For some reason, the String is not getting printed - I don't understand where I'm going wrong. Can somebody please point out what I'm doing wrong?

Thank you very much!

GanTheMan
  • 1
  • 2

2 Answers2

0

Your transferToString method does not return anything. Return the expression variable.

Stack<String> operator= new Stack<String>();
Stack<String> operand= new Stack<String>();
String expression="";

Also note that the declarations above is not needed in the main method, you can put it in the transferToString method. Thus your transferToString method will just accept the parameter input.

This will be the new look of your main method and transferToString method.

main Method :

public static void main(String[]args)
    {
        Scanner s= new Scanner (System.in);

        System.out.println("Enter expression in infix: ");
        String input= s.nextLine();
        System.out.println("Created stacks...");
        String expression = transferToString(input);
        System.out.println("Created string... ");
        System.out.println(expression);

    }

transferToString method:

public static String transferToString(String input){
    Stack<String> operator= new Stack<String>();
    Stack<String> operand= new Stack<String>();
    String expression="";

    // Copy the code of your old transferToString method here

    return expression;
}
Mark Melgo
  • 1,477
  • 1
  • 13
  • 30
0

The issue is that the expression that has scope within main() is never being set to the expression that is being set within transferToString.

Simply set the expression that is defined within main to transferToString, and have transferToString return the newly created expression.

Like so: String expression = transferToString(input, operator, operand, "");

Always read through your code line by line and consider what each line is actually doing, it will solve lots of silly errors!

NotZack
  • 518
  • 2
  • 9
  • 22