Pretty much i can convert from infix to postfix really well and that is nto the problem
i need help with the postfix evaluation
also i dont think my evaluate method is correct
i have two push methods, one for char and one for int.

i really am stuck and not giving it much thought now :( , been working on it for 3 hours
if possible can someone try to help me shape the code if its correct to print the postfix and the the evaluation
but yah first i need to evaluate
also it would be cool if i could just evaluate the input directly .
so basically it is not necessary to evaluate the postfix
all i need is to show the postfix and the evaluation of the input if its too hard to evaluate the postfix.
please do help.
thank you in advance

import java.io.*;            
class StackX
   {
   private int maxSize;
   private char[] stackArray;
   private int[] stackArray1;
   private int top;

   public StackX(int s)       
      {
      maxSize = s;
      stackArray = new char[maxSize];
      top = -1;
      }

public void push(char j)  
      { stackArray[++top] = j; }

public void push1(int j)
{ stackArray1[++top] =j ; }

public char pop()         
      { return stackArray[top--]; }

   public char peek()        
      { return stackArray[top]; }

   public boolean isEmpty()  
      { return (top == -1); }

   public int size()         
      { return top+1; }

   public char peekN(int n)  
      { return stackArray[n]; }



   }  // end class StackX used from lab1

class InToPost                  // infix to postfix conversion borrowed from book
   {
   private StackX theStack;
   private String input;
   private String output = "";

   public InToPost(String in)   // constructor
      {
      input = in;
      int stackSize = input.length();
      theStack = new StackX(stackSize);
      }

   public String doTrans()      // algorithm that does translation with cases
      {
      for(int j=0; j<input.length(); j++)      
         {
         char ch = input.charAt(j);          

         switch(ch)
            {case '+':gotOper(ch, 1); break;              
             case '-':gotOper(ch, 1); break;                                        
             case '*':gotOper(ch, 2); break;                  
             case '/':gotOper(ch, 2); break;
             default :output = output + ch; break;  //writes to output                      
           }  // end of switch
         }  // end for loop
      while( !theStack.isEmpty() )     // pop remaining operators at the end
         {
          output = output + theStack.pop(); // write to output
         }
         return output;                   // return postfix
      }  
//--------------------------------------------------------------
   public  void gotOper(char opThis, int prec1)
      {                                
      while( !theStack.isEmpty() )
         {
         char opTop = theStack.pop();

        if(1==1 )                          
            {
            int prec2;                 

            if(opTop=='+' || opTop=='-')  // find new op prec
               prec2 = 1;
            else
               prec2 = 2;
            if(prec2 < prec1)          
               {                       
               theStack.push(opTop);   
               break;
               }
            else                       
               output = output + opTop;  
            }  
         }  // end while loop
      theStack.push(opThis);           
      }  
   public void evaluate() {
        int x;
        int y;
        for (int j = 0; j < output.length(); j++) {
            int result = 0;
            char ch = output.charAt(j);
            switch (ch) {
                case '+':
                    x = theStack.pop();
                    y = theStack.pop();
                    result = x + y;
                    theStack.push1(result);
                    break;
                case '-':
                    x = theStack.pop();
                    y = theStack.pop();
                    result = x - y;
                    theStack.push1(result);
                    break;
                case '*':
                    x = theStack.pop();
                    y = theStack.pop();
                    result = x * y;
                    theStack.push1(result);
                    break;
                case '/':
                    x = theStack.pop();
                    y = theStack.pop();
                    result = x / y;
                    theStack.push1(result);
                    break;
                default:
                    theStack.push(ch);
                    break;
            }
        }
        System.out.println(theStack.pop());
    }

   }  

class lab2
   {
   public static void main(String[] args) throws IOException
      {
      String input, output;
      while(true)
         {
         System.out.print("Enter infix: ");
         System.out.flush();
         input = getString();         
         if( input.equals("") )       
            break;

         InToPost theTrans = new InToPost(input);
         output = theTrans.doTrans(); 




            System.out.println("Postfix is " + output  );
         }  
      }  

   public static String getString() throws IOException
      {
      InputStreamReader isr = new InputStreamReader(System.in);
      BufferedReader br = new BufferedReader(isr);
      String s = br.readLine();
      return s;
      }

   }  
verruckt24 commented: 49 posts and no code tags, learn the rules fast +0

well i put some thought to it
i have new code
all i need now is some help calling the method correctly
also here is the error that i am getting

The method evaluate(String) is undefined for the type lab2

import java.util.*;
import java.io.*;            
class StackX
   {
   private int maxSize;
   private char[] stackArray;
   private int[] stackArray1;
   private int top;

   public StackX(int s)       
      {
      maxSize = s;
      stackArray = new char[maxSize];
      top = -1;
      }

public void push(char j)  
      { stackArray[++top] = j; }

public void push1(int j)
{ stackArray1[++top] =j ; }
   
public char pop()         
      { return stackArray[top--]; }
public int pop1()         
{ return stackArray[top--]; }
   public char peek()        
      { return stackArray[top]; }

   public boolean isEmpty()  
      { return (top == -1); }

   public int size()         
      { return top+1; }

   public char peekN(int n)  
      { return stackArray[n]; }

  

   }  // end class StackX used from lab1

class InToPost                  // infix to postfix conversion borrowed from book
   {
   private StackX theStack;
   private String input;
   private String output = "";

   public InToPost(String in)   // constructor
      {
      input = in;
      int stackSize = input.length();
      theStack = new StackX(stackSize);
      }

   public String doTrans()      // algorithm that does translation with cases
      {
      for(int j=0; j<input.length(); j++)      
         {
         char ch = input.charAt(j);          
         
         switch(ch)
            {case '+':gotOper(ch, 1); break;              
             case '-':gotOper(ch, 1); break;                                        
             case '*':gotOper(ch, 2); break;	              
             case '/':gotOper(ch, 2); break;
             default :output = output + ch; break;  //writes to output                      
           }  // end of switch
         }  // end for loop
      while( !theStack.isEmpty() )     // pop remaining operators at the end
         {
          output = output + theStack.pop(); // write to output
         }
         return output;                   // return postfix
      }  
//--------------------------------------------------------------
   public  void gotOper(char opThis, int prec1)
      {                                
      while( !theStack.isEmpty() )
         {
         char opTop = theStack.pop();
         
        if(1==1 )                          
            {
            int prec2;                 

            if(opTop=='+' || opTop=='-')  // find new op prec
               prec2 = 1;
            else
               prec2 = 2;
            if(prec2 < prec1)          
               {                       
               theStack.push(opTop);   
               break;
               }
            else                       
               output = output + opTop;  
            }  
         }  // end while loop
      theStack.push(opThis);           
      }  
   	}

     

class lab2
   {
   public static void main(String[] args) throws IOException
      {
	 String output;
      String input;
      while(true)
         {
         System.out.print("Enter infix: ");
         System.out.flush();
         input = getString();         
         if( input.equals("") )       
            break;
                                      
         InToPost theTrans = new InToPost(input);
         output = theTrans.doTrans(); 
 
     	
     	
         System.out.println("Evaluated expression: " + (output));

         System.out.println("Evaluated expression: " + evaluate(theTrans.doTrans(output)));

     	 System.out.println("Postfix is " +evaluate(output)) ;
         }  
      }  
public class Eval{
 private StackX operatorStack ;
	private StackX operandStack;
  public  int evaluate(  String output)
	{
		StringTokenizer s = new StringTokenizer(output);//divides into tokens
		
		int value;
		String symbol;
		while (s.hasMoreTokens())
		{
			symbol = s.nextToken();
			if (Character.isDigit(symbol.charAt(0)))// if its a number push it
			
			{
				Integer operand = new Integer(Integer.parseInt(symbol));
				operandStack.push1(operand);
			}
			else // if it's an operator, operate on the previous two popped operandStack items
			{
				int op2 = ((Integer)operandStack.pop1()).intValue();
				int op1 = ((Integer)operandStack.pop1()).intValue();
				int result = 0;
				switch(symbol.charAt(0))
				{
					case '*': {result = op1 * op2; break;}
					case '+': {result = op1 + op2; break;}
					case '-': {result = op1 - op2; break;}
					case '/': {result = op1 / op2; break;}
					case '%': {result = op1 % op2; break;}
				}
				Integer operand = new Integer(result);
				operandStack.push1(operand);
			}
		}
		value = ((Integer)operandStack.pop1()).intValue();
		return value;
	}

}

public static String getString() throws IOException
      {
      InputStreamReader isr = new InputStreamReader(System.in);
      BufferedReader br = new BufferedReader(isr);
      String s = br.readLine();
      return s;
      }

   }

This amount of code is impossible to read!

Test your stack before attempting to use it!

hey... can I use this program? Because I really need it to my project about Infix to postfix conversion... (^_^) it runs perfectly... hehehe...

@notuserfriendly : The logic for postfix evaluation is pretty straightforward. Move over the postfix string/expression and push any operands (numbers) into the stack, whenever you find an operator pop the topmost two operands from the stack and evalulate them, then put the result back onto the stack, continue moving over the string in the same manner until you have reached the end, when you reach the end of the expression, pop the lone value from the stack. This is your result.

bah i solved it myself
forgot to mark it solved
no the code is right but all i need was a getStringvalue to display the last print

thatz a good one

Closing this year long dead zombie.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.