Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Using a Switch statement for Infix to Prefix Expressions

807588Mar 11 2008 — edited Jun 4 2009
I am stuck on the numeric and operator portion of the switch statement...I have the problem also figured out in an if/else if statement and it works fine, but the requirements were for the following algorithm:

while not end of expression
switch next token of expression
case space:
case left parenthesis:
skip it
case numeric:
push the string onto the stack of operands
case operator:
push the operator onto the stack of operators
case right parenthesis:
pop two operands from operand stack
pop one operator from operator stack
form a string onto operand stack
push the string onto operand stack
pop the final result off the operand stack

I know that typically case/switch statement's can only be done via char and int's. As I said I am stuck and hoping to get some pointers. This is for a homework assignment but I am really hoping for a few pointers. I am using a linked stack class as that was also the requirements. Here is the code that I have:
   import java.io.*;
   import java.util.*;
   import java.lang.*;
	
 /*--------------------------- PUBLIC CLASS INFIXTOPREFIX --------------------------------------*/
 /*-------------------------- INFIX TO PREFIX EXPRESSIONS --------------------------------------*/
 
    public class infixToPrefix {
   
      private static LinkedStack operators = new LinkedStack();
      private static LinkedStack operands = new LinkedStack();
   
   	// Class variable for keyboard input
      private static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
   
   	 // Repeatedly reads in infix expressions and evaluates them
       public static void main(String[] args) throws IOException {
       
      // variables
         String expression, response = "y";
      
      // obtain input of infix expression from user
         while (response.charAt(0) == 'y') {
            System.out.println("Enter a parenthesized infix expression.");		// prompt the user
            System.out.println("Example: ( ( 13 + 2 ) * ( 10 + ( 8 / 3 ) ) )");
            System.out.print("Or as: ((13+2)*(10+(8/3))):  ");
            expression = stdin.readLine();	// read input from the user
         	         	
         // output prefix expression and ask user if they would like to continue     	
            System.out.println("The Prefix expression is: " + prefix(expression));	// output expression
            System.out.println("Evaluate another? y or n: ");		// check with user for anymore expressions
            response = stdin.readLine();	// read input from user
            if (response.charAt(0) == 'n') {		// is user chooses n, output the statement
               System.out.println("Thank you and have a great day!");
            }	// end if statement
         	
         }	// end while statement
      }	// end method main
   
   
   /*------------- CONVERSION OF AN INFIX EXPRESSION TO A PREFIX EXPRESSION ------------*/  
   /*--------------------------- USING A SWITCH STATEMENT ------------------------------*/
   
       private static String prefix(String expression) {
       	// variables
         String symbol, operandA, operandB, operator, stringA, outcome;
      	// initialize tokenizer
         StringTokenizer tokenizer = new StringTokenizer(expression, " +-*/() ", true);	
      
         while (tokenizer.hasMoreTokens()) {
            symbol = tokenizer.nextToken();	// initialize symbol	
            switch (expression) {
               case ' ':
                  break;	// accounting for spaces
               case '(':
                  break;	// skipping the left parenthesis
               case (Character.isDigit(symbol.charAt(0))): 	// case numeric 
                  operands.push(symbol);							// push the string onto the stack of operands
                  break;
               case (!symbol.equals(" ") && !symbol.equals("(")):	// case operator
                  operators.push(symbol);									// push the operator onto the stack of operators
                  break;
               case ')':
                  operandA = (String)operands.pop();	// pop off first operand 
                  operandB = (String)operands.pop();	// pop off second operand
                  operator = (String)operators.pop();	// pop off operator
                  stringA = operator + " " + operandB + " " + operandA;		// form the new string
                  operands.push(stringA);
                  break;
            }	// end switch statement
         
         }	// end while statement
      		
         outcome = (String)operands.pop();	// pop off the outcome
      
         return outcome;	// return outcome
      }	// end method prefix
   }	// end class infixToPrefix
Any help would be greatly appreciated!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 2 2009
Added on Mar 11 2008
11 comments
736 views