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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:55:14+00:00 2026-05-21T06:55:14+00:00

Hi guys i’m almost done writing my code and i’m stuck with this stupid

  • 0

Hi guys
i’m almost done writing my code and i’m stuck with this stupid thing. I can identify cases in which there’s a unary minus before brackets (-[4 + 4]). here’s my code:

package oop.ex2.expression;

import java.io.IOException;
import java.util.HashMap;
import oop.ex2.exception.*;
import oop.ex2.main.Tokenizer;

/**
 * This class contains 3 public static methods. All 3 methods are used
 * to parse text into a mathematical expression. The information is "thrown"
 * back and forth from one method to another.
 */
public class ExpressionParser {

    /**
     * This method uses expression() method to parse the text into mathematical
     * expressions, and returns an expression which is the sum of all
     * expressions returned from expression() [the sum is calculated according
     * to the right operator]
     *
     * @param st - the Tokenizer parsing the text
     * @return - Expression, the sum of all expressions from expression()
     * @throws InputException
     * @throws IOException
     */
    public static Expression sumExpressions(Tokenizer st)
            throws InputException, IOException {
        boolean endOfLine = false;
        Expression temp = expression(st);
        int token = Tokenizer.TT_NOTHING;
        while (!endOfLine) {
            token = st.nextToken();
            if ((token == Tokenizer.TT_OPERATOR)
                    || (token == Tokenizer.TT_OVERLOADED_OP))
                temp = new FatherExpression(st.op, temp, expression(st));
            else
                endOfLine = true;
        }
        return temp;

    }

    public static Expression expression(Tokenizer st) throws InputException, IOException {
        Expression result = null;
        switch (st.nextToken()) {
            case Tokenizer.TT_NUMBER:
                result = new NumberExpression(st.nval);
                break;
            case Tokenizer.TT_VARIABLE:
                result = new VariableExpression(st.sval);
                break;
            case Tokenizer.TT_FUNC:
                result = createFunction(st);
                break;
            case '[':
                result = sumExpressions(st);
                if (st.ttype != ']')
                    throw new BracketException("BracketException: "
                            + "one too many ']'");
                break;
            default:
                throw new UnexpectedTokenException("Unexpected token on" +
                        "ExpressionParser.elements(st)");
        }
        return result;
    }

    private static Expression createFunction(Tokenizer st)
            throws IOException, InputException {
        if (InlineManager.getAllInlineFunctions().containsKey(st.sval)) {
            InlineFunction temp = InlineManager.getInlineFunction(st.sval);
            temp.setArguments(st);
            return temp;
        }
        if (st.sval.equals("MAX"))
            return new Max(st);
        if (st.sval.equals("MIN"))
            return new Min(st);
        if (st.sval.equals("POW"))
            return new Pow(st);
        if (st.sval.equals("MOD"))
            return new Mod(st);
        if (st.sval.equals("ABS"))
            return new Abs(st);
        throw new FunctionNameException("Wrong funcion entred " + st.sval);
    }

    public static HashMap<String, Expression> parseArguments(Tokenizer st)
            throws IOException, InputException {
        HashMap<String, Expression> result = new HashMap<String, Expression>();
        if (st.nextToken() != '{')
            throw new UnexpectedTokenException("Missing {");
        int argument = 0;
        while (true) {
            st.ignoreToken(',', true);
            switch (st.nextToken()) {
                case '}':
                    st.ignoreToken(',', false);
                    return result;
                case '[':
                    result.put(String.valueOf(argument++), sumExpressions(st));
                    break;
                case Tokenizer.TT_NUMBER:
                    result.put(String.valueOf(argument++), new NumberExpression(st.nval));
                    break;
                case Tokenizer.TT_VARIABLE:
                    result.put(String.valueOf(argument++), new VariableExpression(st.sval));
                    break;
                case Tokenizer.TT_FUNC:
                    result.put(String.valueOf(argument++), createFunction(st));
                    break;
                default:
                    throw new UnexpectedTokenException("Unexpected token on function arguments");
            }
        }
    }
}

it long i know. Expression object could be a constant, a variable or a function such as MAX{3,2} which is 3. expression() uses a tokenizer i have built to parse text into an expression, and sumExpression() uses expression() to create a new Expression which is a combination of two Expression object according to the right operator.

i hope its clear. as i said before i can’t figure out how to identify the unary minus (-[4] would be -4) thing. i didn’t put my tokenizer code, didn’t think its necessary.

thanks!

P.S.
the order of calculations is defined to be left to right with no regards to type of operator.

  • 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-21T06:55:14+00:00Added an answer on May 21, 2026 at 6:55 am

    The difference between (prefix) unary and (infix) binary operators is the context in which they occur. A binary operator always follows an expression, while a unary operator occurs at a position where an expression is expected, i.e. at the start, after an operator or after an opening parenthesis.

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

Sidebar

Related Questions

Guys, I’ve been writing code for 15+ years, but managed to avoid Web Development
Guys, I've came across this problem I can't resolve myselg, I'm pretty sure I
guys i have arrays in which i have to match this kind of text
guys. I have UITableView with different cells and I have code, which counts height.
guys i have a xml file which is like this: <Point TestFlag=0 id=1 name=Conversation
Guys this code is returning false in a blank page after submit.What is the
Guys, this can't be for real I'm trying to make a .NET 2.0 executable
guys. I tried this code: def trap_check(payroll[][], timelive[][]) . . . end I was
Guys How can I make this work `find /xyz/abc/music/ |grep def` I don't want
Guys I am becoming completely nut on this...and can't figure out at all how

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.