Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6586841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:50:33+00:00 2026-05-25T16:50:33+00:00

Hey Guys I’m having a problem when I run my program. In the PostfixEvaluate()

  • 0

Hey Guys I’m having a problem when I run my program. In the PostfixEvaluate() Method is where it takes in a string and solves the postfix problem and returns it. Well when I go to run it, I’m getting a bunch of random numbers(some repeated), I’m going crazy because I don’t know what else to try and I’ve spent more time on this than it should normally take.

Heres the PostfixEvaluate Method:

   public int PostfixEvaluate(String e){
        //String Operator = "";
    int number1;
        int number2;
        int result=0;
        char c;
        //number1 = 0;
        //number2 = 0;

        for(int j = 0; j < e.length(); j++){
            c = e.charAt(j);
            if (c != '+'&& c!= '*' && c!= '-' && c!= '/') {
                //if (c == Integer.parseInt(e)) {   
                s.push(c); 
        }
            else {
                number1 = s.pop();
                number2 = s.pop();
                switch(c) {
                    case '+':
                    result = number1 + number2;
                    break;
                    case '-':
                    result = number1 - number2;
                    break;
                    case '*':
                    result = number1 * number2;
                    break;
                    case '/':
                    result = number1 / number2;
                    break;
                    } s.push(result);
                }
            System.out.println(result);
        } 
            return s.pop();
}

public static void main(String[] args) {
    Stacked st = new Stacked(100);
    String y = new String("(z * j)/(b * 8) ^2");
    String x = new String("2 3 + 9 *");
    TestingClass clas = new TestingClass(st);

    clas.test(y);


    clas.PostfixEvaluate(x);




 }
        }

This is the Stack Class:

 public class Stacked  {

    int top;
    char stack[];
    int maxLen;

    public Stacked(int max) {
        top = -1; 
        maxLen = max; 
        stack = new char[maxLen];

        }

    public void push(int result) {
            top++;
            stack[top] = (char)result;

        }

    public int pop() {
        int x;
        x = stack[top];
        //top = top - 1;
        top--;

        return x;

    }

    public boolean isStackEmpty() {
            if(top == -1) {
                System.out.println("Stack is empty " + "Equation Good");

                return true;
            } 
            else 
                System.out.println("Equation is No good");
                return false;
    }

    public void reset() {

        top = -1;
    }

    public void showStack() {
        System.out.println(" ");
        System.out.println("Stack Contents...");
        for(int j = top; j > -1; j--){
            System.out.println(stack[j]);
        }
        System.out.println(" ");
    }

    public void showStack0toTop() {
        System.out.println(" ");
        System.out.println("Stack Contents...");
        for(int j=0; j>=top; j++){
            System.out.println(stack[j]); 
        }
        System.out.println(" ");
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:50:33+00:00Added an answer on May 25, 2026 at 4:50 pm

    You need to split the string into tokens first:

    /* Splits the expression up into several Strings,
     * all of which are either a number or and operator,
     * none of which have spaces in them. */
    String [] expressionAsTokens = e.split(" ");
    

    Then you need to make sure you compare Strings, not chars:

    //compare strings instead of chars
    String token = expressionAsTokens[j];
    if (!"+".equals(token) && !"*".equals(token) && !"-".equals(token) && !"/".equals(token)) {
        s.push(Integer.parseInt(token)); 
    } else {
        //same code as you had before
    }
    

    Also, is there any reason you are storing everything as a char array in your Stacked class? Your pop() method returns and integer, yet everything is stored as a char.

    For this application, everything should be stored as an integer:

    public class Stacked {
        int stack[]; // array is of type integer
        int top;
        int maxLen;
    
        // constructor
    
        public void push() {/*...*/}
        public int pop() {/*...*/} //pop returns an int as before
    
        //...
    }
    

    One final note: Be careful what order you add and subtract the numbers in. I don’t remember if postfix operands are evaluated left first or right first, but make sure you get them in the right order. As you have it now, 2 3 - 4 * would evaluate as 4 * (3 - 2) and I think it should be (2 - 3) * 4. This won’t matter with adding and multiplying, but it will with subtracting and dividing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys, I'm having a problem with IE7, my menu always drops down behind
Hey guys, i'm working on a program that gets a postfix expression and calculates
Hey guys i want to execute my SQL statement but im having synatx trouble,
Hey guys, I'm using Twitter's PHP API, called twitterlibphp , and it works well,
Hey guys, working on an event calendar. I'm having some trouble getting my column
Hey Guys, having an issue with Linq-to-SQL, I am basically doing the following but
Hey guys, I'm currently trying to implement a function using C that takes in
Hey guys, I'm trying to select a specific string out of a text, but
Hey guys! I have this little problem: I have one ViewController which adds 2
Hey guys, I have a problem (again). This time I am trying to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.