Infix to postfix using array stacks

Thread Solved

Join Date: Nov 2008
Posts: 478
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 41
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training

Infix to postfix using array stacks

 
0
  #1
Mar 3rd, 2009
Hello. I'm trying to write a program that will convert a user input infix expression into its postfix form. My current code is allowing the user to input a string, but it does nothing with the string. I'm fairly new to JAVA, thus I think I'm making a simple mistake in the main method. I'm using a generic class for a stacks implemented using arrays to store any data type. Thanks in advance for any help.
<code>
import java.io.*;
import java.util.*;
//begin coding for the stack interface
interface Stack<E>
{
public boolean isEmpty();//tests is current stack is empty. Returns true if so, and false if not.
public E top() throws StackException;//retrieves value at the top of the stack. Stack cannot be empty.
public void push(E value) throws StackException;//pushes a value on the top of the stack.
public void pop() throws StackException;//removes a value from the top of the stack. Stack cannot be empty.
}//terminates coding of Stack interface

//begin coding for the objArrayStack class
class objArrayStack<E> implements Stack<E>
{
//constructor
public objArrayStack()
{
topValue=-1;
}//terminates constructor
public void push(E value)throws StackException
{
if(topValue<ArraySize-1)//currrent stack is not full
{
++topValue;
Info[topValue]=value;
}//terminates if
else //current stack is full
throw new StackException("Error: Overflow");
}//terminates push method
public void pop() throws StackException
{
if(!isEmpty())//current stack is not empty
--topValue;
else //stack is empty
throw new StackException("Error: Underflow");
}//terminates pop method
public boolean isEmpty()
{
return topValue==-1;
}//terminates isEmpty method
public E top() throws StackException
{
if(!isEmpty())//stack is not empty
return (E)Info[topValue];
else //stack is empty
throw new StackException("Error: Underflow");
}//terminates top method
//declare instance variables
final int ArraySize=10;
private Object Info[]=new Object[ArraySize];
private int topValue;

//begins coding for the StackException class
class StackException extends RuntimeException
{
//constructor
public StackException(String str)
{
super(str);
}//terminates text of constructor
}//terminates text of StackException class

//method to convert from infix to postfix notation
public static String InToPost(String infixString)
{
//operator stack initialized
objArrayStack<Character> operatorStack = new objArrayStack<Character>();
//postfix string initialized as empty
String postfixString = " ";
//scan infix string and take appropriate action
for(int index = 0; index < infixString.length(); ++index)
{
char chValue = infixString.charAt(index);
if(chValue == '(')
operatorStack.push('(');
else if(chValue == ')')
{
Character oper = operatorStack.top();
while(!(oper.equals('(')) && !(operatorStack.isEmpty()))
{
postfixString += oper.charValue();
operatorStack.pop();
oper = operatorStack.top();
}//end while loop
operatorStack.pop();
}//end else if
else if(chValue == '+' || chValue == '-')
{
if(operatorStack.isEmpty()) //operatorStack is empty
operatorStack.push(chValue);
else //current operatorStack is not empty
{
Character oper = operatorStack.top();
while(!(operatorStack.isEmpty() || oper.equals(new Character('(')) || oper.equals(new Character(')'))))
{
operatorStack.pop();
postfixString += oper.charValue();
}//ends while loop
operatorStack.push(chValue);
}//end else
}//end else if
else if(chValue == '*' || chValue == '/')
{
if(operatorStack.isEmpty())
operatorStack.push(chValue);
else
{
Character oper = operatorStack.top();
while(!oper.equals(new Character('+')) && !oper.equals(new Character('-')) && !operatorStack.isEmpty())
{
operatorStack.pop();
postfixString += oper.charValue();
}//end while loop
operatorStack.push(chValue);
}//end else
}//end else if
else
postfixString += chValue;
}//end for loop
while(!operatorStack.isEmpty())
{
Character oper = operatorStack.top();
if(!oper.equals(new Character('(')))
{
operatorStack.pop();
postfixString += oper.charValue();
}//end if
}//end while
return postfixString ;
}//terminates text of InToPost method

public static void main(String[]args)
{
objArrayStack mystack = new objArrayStack();
System.out.println("Enter a string");
Scanner scan = new Scanner(System.in);
scan.nextLine();
String str = scan.nextLine();
InToPost(str);
}//terminates text of main method
}//terminates text of objArrayStack class
</code>
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 151
Reputation: GDICommander is an unknown quantity at this point 
Solved Threads: 19
GDICommander's Avatar
GDICommander GDICommander is offline Offline
Junior Poster

Re: Infix to postfix using array stacks

 
0
  #2
Mar 3rd, 2009
Use System.out.println("Your message") to print debugging messages about your input string and the value of every variable. First, ensure that the string is received from the scanner. After, look in the code of the stack.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 903
Reputation: verruckt24 is just really nice verruckt24 is just really nice verruckt24 is just really nice verruckt24 is just really nice verruckt24 is just really nice 
Solved Threads: 78
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Posting Shark

Re: Infix to postfix using array stacks

 
0
  #3
Mar 4th, 2009
  1. scan.nextLine();
  2. String str = scan.nextLine();

This is what causes the problem. You are already moving your scanner past the line read. Remove the first scanner.nextLine();
Read the Scanner docs here for more details.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 74
Reputation: notuserfriendly is an unknown quantity at this point 
Solved Threads: 5
notuserfriendly notuserfriendly is offline Offline
Junior Poster in Training

Re: Infix to postfix using array stacks

 
0
  #4
Mar 4th, 2009
u need to implement the stack
then do precedence with switch cases
ill give more info once i get home, but at least show some code that you wrote not the code from your resource material form CD
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 478
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 41
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training

Re: Infix to postfix using array stacks

 
0
  #5
Mar 4th, 2009
Originally Posted by notuserfriendly View Post
u need to implement the stack
then do precedence with switch cases
ill give more info once i get home, but at least show some code that you wrote not the code from your resource material form CD
I implemented the stack interface and the class containing all of the methods(pop, push, etc.) and I'm pretty sure there are no errors there. I wrote this code myself, I didn't copy it from a CD or website. Also, I know for a fact that converting from infix to postfix can be done without using a switch statement. Thanks for the reply though.
Check out my new band Apollo's Soundtrack on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 903
Reputation: verruckt24 is just really nice verruckt24 is just really nice verruckt24 is just really nice verruckt24 is just really nice verruckt24 is just really nice 
Solved Threads: 78
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Posting Shark

Re: Infix to postfix using array stacks

 
0
  #6
Mar 5th, 2009
Originally Posted by notuserfriendly View Post
u need to implement the stack
then do precedence with switch cases
ill give more info once i get home, but at least show some code that you wrote not the code from your resource material form CD
Why is that, that you believe he copied the code ?

@OP : Read my prvious post I've underlined the problem there.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 478
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 41
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training

Re: Infix to postfix using array stacks

 
0
  #7
Mar 5th, 2009
Thanks for backing me up, verruckt24. It is good to see that not everyone in this forum is so cynical and suspicious. I made some ajustments to my program and got it to work. Thanks for your help.
Check out my new band Apollo's Soundtrack on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 903
Reputation: verruckt24 is just really nice verruckt24 is just really nice verruckt24 is just really nice verruckt24 is just really nice verruckt24 is just really nice 
Solved Threads: 78
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Posting Shark

Re: Infix to postfix using array stacks

 
0
  #8
Mar 5th, 2009
Has your query been solved, if yes then please mark the thread so, so people don't keep posting here for ages
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 478
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 41
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training

Re: Infix to postfix using array stacks

 
0
  #9
Mar 11th, 2009
If anyone needs help with this particular problem (converting infix to postfix and evaluating), send me a message. I have a working version of the program and I'd be glad to help.
Check out my new band Apollo's Soundtrack on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 903
Reputation: verruckt24 is just really nice verruckt24 is just really nice verruckt24 is just really nice verruckt24 is just really nice verruckt24 is just really nice 
Solved Threads: 78
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Posting Shark

Re: Infix to postfix using array stacks

 
0
  #10
Mar 11th, 2009
Instead of offering them the program, let them learn the way you did by, putting pieces together and asking wherever he's stuck. Plus it would certainly be of more benefit to all learners if it is kept on the forum. Not that he cannot PM you asking for help, but keeping it on the forum is always better for the community.

Also if you feel you have a piece of code worth sharing you can always add to the code snippets on the forum. This way the work of programming has a larger audience and user base.
Last edited by verruckt24; Mar 11th, 2009 at 1:13 pm.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 3126 | Replies: 10
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC