Results 1 to 3 of 3
  1. #1
    dalangley is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default Postfix Evaluation using a Stack, Help?

    Alright so my task is to design a driver that accepts postfix expressions in the form of:

    BC+
    ACE^
    A/B
    etc.

    First off, I must read input from the user in that form and get the integer value of the letters where:

    public static int intValue(char letter)
    {
    int value =0;
    if ( letter >= 'A' || letter <= 'Y')
    value = (int) letter - 64;
    else if (letter == 'Z')
    value = 0;
    return value;
    }


    Basically 'A' = 1, 'B' = 2, etc with 'Z' = 0.

    I have gotten this far, but to no avail. I type in an expression such as BC+ and it does nothing but give me these errors:

    Too few operands or too many operators.
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
    at java.lang.String.charAt(String.java:687)
    at StackClient.main(StackClient.java:52)



    import java.util.Scanner;

    public class StackClient {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args)
    {
    char operator = 0;
    String expression;
    int result = 0, loc = 0;

    System.out.println("\t\tProgram #2 Stacks\n\nThis program will read expressions from user input into\n" +
    "a stack where the given expressions will be evaluated with different operators.\n" +
    "It will also employ the use of push and pop to mutate and access the data in" +
    "the stack.\n");
    Stack theStack = new Stack(10);
    Scanner scan = new Scanner(System.in);

    System.out.println("Please enter an expression in postfix form using letters (A,B,C, etc.):");
    expression = scan.next();

    while(true)
    {


    if (theStack.isEmpty())
    System.out.println("Too few operands or too many operators.");
    else
    theStack.pop();

    if (theStack.isNotEmpty())
    System.out.println("Too many operands.");
    theStack.flush();

    for (int i = loc; i <= expression.length(); i++)
    theStack.push(theStack.intValue((char) expression.charAt(i)));
    theStack.push(expression.charAt(loc));

    if (expression.charAt(loc) == operator)
    theStack.push(expression.charAt(loc));

    switch (operator)
    {
    case '+': result = theStack.pop() + theStack.pop();

    case '-': result = theStack.pop() - theStack.pop();

    case '/': result = theStack.pop() / theStack.pop();

    case '%': result = theStack.pop() % theStack.pop();

    case '^': result = theStack.pop() * theStack.pop();

    }


    Where am I messing up? I need the program to take in those expressions and evaluate in postfix form. Do I have the loops wrong? Am I not searching the expression right? Any ideas?

  2. #2
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    EDIT: I had an idea but was wrong when I tested. Sorry.
    -MK12
    Last edited by MK12; 02-16-2009 at 11:45 PM.
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  3. #3
    dalangley is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default

    The method intValue accepts a char type paramater and typecasts it in the evalutation to return the ascii value minus 64. Says so in the method. I am positive that works. Ive tested that much. I need to evaluate the postfix expression using a Stack.

Similar Threads

  1. [SOLVED] Boolean Expression Evaluation Framework
    By priyanka.dandekar in forum Advanced Java
    Replies: 8
    Last Post: 03-27-2010, 03:35 PM
  2. Help on Stack Implementation
    By danver_2009 in forum New To Java
    Replies: 1
    Last Post: 02-16-2009, 09:12 AM
  3. Postfix-Notation
    By little_polarbear in forum New To Java
    Replies: 9
    Last Post: 09-09-2008, 05:24 PM
  4. Replies: 0
    Last Post: 04-15-2008, 08:36 PM
  5. How to parse postfix arithmetic expressions
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 08:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •