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 4088592
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:00:09+00:00 2026-05-20T19:00:09+00:00

I’ve got a program that goes through and reads tokens that can either be

  • 0

I’ve got a program that goes through and reads “tokens” that can either be a String (Symbol) or a number. It uses postfix and a stack to evaluate simple commands.

For example:

/x 100 def
/y 200 def
x y add

should return 300. The first line defines a variable called “x” and sets it to 100. To do this a reader adds “/x” and “100” on the stack and stops when it gets to the “def” operator, which tells it to go make a token with a Symbol called “x” and its value being 100. The stack is then empty, and next time “x” would be pushed, the interpreter should automatically replace it with its value. This is where my problem lies.

This is my interpreter:

while ( r.hasMoreTokens() ) {
            Token t = r.nextToken();

            if ( !t.isSymbol() ) {
                operands.push( t );
            } else if (env.contains(t.getSymbol())) {
                Token tmp = env.get(t.getSymbol());
                operands.push(tmp); 
            } else if (t.getSymbol().startsWith("/")) {
                operands.push(t);
            } else if ( t.getSymbol().equals( "def" ) ){
                execute_def();
            } else if ( t.getSymbol().equals( "add" ) ) {
                execute_add();
            } else if ( t.getSymbol().equals( "sub" ) ) {
                execute_sub();
            } else if ( t.getSymbol().equals( "mul" ) ) {
                execute_mul();
            } else if ( t.getSymbol().equals( "exch" ) ) {
                execute_exch();
            } else if ( t.getSymbol().equals( "dup" ) ) {
                execute_dup();
            } else if ( t.getSymbol().equals( "pop" ) ) {
                execute_pop();
            } else if ( t.getSymbol().equals( "pstack" ) ) {
                execute_pstack();
            } else if ( t.getSymbol().equals( "moveto" ) ) {
                execute_moveto();
            } else if ( t.getSymbol().equals( "lineto" ) ) {
                execute_lineto( g );
            } else if ( t.getSymbol().equals( "arc" ) ) {
                execute_arc( g );
            } else if ( t.getSymbol().equals( "quit" ) ) {
                execute_quit();
            } else {
                System.out.println( "ILLEGAL SYMBOL: " + t );
            }
        }

Once the variables get defined correctly, I cant get into that first else if and change the value. Because I can’t do this, I never push anything on the stack and end up with an empty stack error. Here are the methods contains() and get() from env (environment):

public boolean contains(String key) {
        Elem tmp = top;
        for (int i = 0; i < size; i++) {
            if (tmp.key == key) {
                return true;
            } else {
                tmp = tmp.next;
            }
        }
        return false;
    }

public Token get(String key) {
        Elem tmp = top;
        int counter = 0;
        boolean found = false;
        for (int i = 0; i < size; i++) {
            if (tmp.key == key) {
                found = true;
                break;
            } else {
                tmp = tmp.next;
            }
            counter++;
        }

        if (found == true) {
            tmp = top;
            for (int i = 0; i <= counter; i++) {
                tmp = tmp.next;
            }
            return tmp.value;
        } else {
            throw new BadKeyQueryException();
        }
    }

I’m using linked elements in the environment to keep track of symbols. Elem is a nested class in Environment:

private static class Elem {
        private String key;
        private Token value;
        private Elem next;

        private Elem(String key, Token value, Elem next) {
            this.key = key;
            this.value = value;
            this.next = next;
        }
    }

Thanks for any help from you guys!

  • 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-20T19:00:10+00:00Added an answer on May 20, 2026 at 7:00 pm

    Strings in java are Objects, rather than primitives.

    When you say:

    int i = 5;
    

    i stores the value “5”.

    When you say:

    String s = "string";
    

    s stores the value of a reference to “string”.

    Comparing s to “string” would return false, even though they contain the same value when you print there. This is because the computer compares a reference to memory containing “string” to another reference to memory containing “string”. Same values, but different references.

    Also, you’re setting “t” to multiple different values in your code. Try setting t once, before everything, and a precomputed t value against your if-else-if block.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
Does anyone know how can I replace this 2 symbol below from the string
i got an object with contents of html markup in it, for example: string
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to loop through a bunch of documents I have to put
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I have just tried to save a simple *.rtf file with some websites and
I have a JSP page retrieving data and when single or double quotes are

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.