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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:38:48+00:00 2026-06-14T18:38:48+00:00

Hi I have managed to fix the null pointer issue. Now I have almost

  • 0

Hi I have managed to fix the null pointer issue.

Now I have almost got the code working except when it is evaluating I am getting the answer for every equation as 15?

My TestClass is shown below:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class TestClass {

private static final int LEFT_ASSOC = 0;
private static final int RIGHT_ASSOC = 1;
static String OPERATORS1 = "+-*/()";

// Operators
private static final Map<String, int[]> OPERATORS = new HashMap<String, int[]>();
static {
    // Map<"token", []{precedence, associativity}>
    OPERATORS.put("+", new int[] { 2, LEFT_ASSOC });
    OPERATORS.put("-", new int[] { 2, LEFT_ASSOC });
    OPERATORS.put("*", new int[] { 3, LEFT_ASSOC });
    OPERATORS.put("/", new int[] { 3, LEFT_ASSOC });
    OPERATORS.put("(", new int[] {1, RIGHT_ASSOC});
    OPERATORS.put(")", new int[] {1, LEFT_ASSOC});
}

private static boolean isOperator(String token) {
    return OPERATORS.containsKey(token);
}

// Test associativity of operator token
private static boolean isAssociative(String token, int type) {
    if (!isOperator(token)) {
        throw new IllegalArgumentException("Invalid token: " + token);
    }

    if (OPERATORS.get(token)[1] == type) {
        return true;
    }
    return false;
}

// Compare precedence of operators.
private static final int cmpPrecedence(String token1, String token2) {
    if (!isOperator(token1) || !isOperator(token2)) {
        throw new IllegalArgumentException("Invalid tokens: " + token1
                + " " + token2);
    }
    return OPERATORS.get(token1)[0] - OPERATORS.get(token2)[0];
}

public static String[] infixToRPN(String[] inputTokens) {
    myArrayList<String> out = new myArrayList<String>();
    myStack<String> stack = new myStack<String>();
    // For each token
    for (String token : inputTokens) {
        StringTokenizer tokens = new StringTokenizer(token,OPERATORS1,true);
        while (tokens.hasMoreTokens()) {
             token = tokens.nextToken();

        }
        // If token is an operator
        if (isOperator(token)) {
            // While stack not empty AND stack top element
            // is an operator
            while (!stack.isEmpty() && isOperator(stack.peek())) {
                if ((isAssociative(token, LEFT_ASSOC) && cmpPrecedence(
                        token, stack.peek()) <= 0)
                        || (isAssociative(token, RIGHT_ASSOC) && cmpPrecedence(
                                token, stack.peek()) < 0)) {
                    out.add(stack.pop());
                    continue;
                }
                break;
            }
            // Push the new operator on the stack
            stack.push(token);
        }
        // If token is a left bracket '('
        else if (token.equals("(")) {
            stack.push(token); 
        }
        // If token is a right bracket ')'
        else if (token.equals(")")) {
            while (!stack.isEmpty() && !stack.peek().equals("(")) {
                out.add(stack.pop());
            }
            stack.pop();
        }
        // If token is a number
        else {
            out.add(token);
        }
    }
    while (!stack.isEmpty()) {
        out.add(stack.pop());
    }
    String[] output = new String[out.size()];
    return out.toArray(output);
}

public static double RPNtoDouble(String[] tokens) {
    myStack<String> stack = new myStack<String>();

    // For each token
    for (String token : tokens) {
        // If the token is a value push it onto the stack
        if (!isOperator(token)) {
            stack.push(token);
        } else {
            // Token is an operator: pop top two entries
            Double d2 = Double.valueOf(stack.pop());
            Double d1 = Double.valueOf(stack.pop());

            // Get the result
            Double result = token.compareTo("+") == 0 ? d1 + d2 : token
                    .compareTo("-") == 0 ? d1 - d2
                            : token.compareTo("*") == 0 ? d1 * d2 : d1 / d2;

            // Push result onto stack
            stack.push(String.valueOf(result));
        }
    }

    return Double.valueOf(stack.pop());
}

@SuppressWarnings("unused")
static public void main(String[] args) throws IOException {
    File file = new File("testEquations.txt");
    String[] lines = new String[10];

    try {
        FileReader reader = new FileReader(file);
        @SuppressWarnings("resource")
        BufferedReader buffReader = new BufferedReader(reader);
        int x = 0;
        String s;
        while ((s = buffReader.readLine()) != null) {
            lines[x] = s;
            x++;
        }
    } catch (IOException e) {
        System.exit(0);
    }
    // test printing string array
    for (String s : lines) {
        System.out.println("" + s);
        String[] output =infixToRPN(lines);
        System.out.println(RPNtoDouble(output));
    }


    for (String st : lines) {
        if (st != null) {
            StringTokenizer tokens = new StringTokenizer(st,OPERATORS1,true);
            while (tokens.hasMoreTokens()) {
                String token = tokens.nextToken();

            }

        }
    }

}

}

and the output i receive is

49+62*61-36
15.0
4/64
15.0
(53+26)
15.0
0*72
15.0
21-85+75-85
15.0
90*76-50+67
15.0
46*89-15
15.0
34/83-38
15.0
20/76/14+92-15
15.0
5*10/3-1
15.0

This is from the following input file:

49+62*61-36
4/64
(53+26)
0*72
21-85+75-85
90*76-50+67
46*89-15
34/83-38
20/76/14+92-15
5*10/3-1

any help will be much appreciated.

Thans for reading.

  • 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-06-14T18:38:49+00:00Added an answer on June 14, 2026 at 6:38 pm

    If your line numbers are correct this is the line:

    else if (token.equals("(")) {
    

    so obviously token is null. => Check if that is the fact, and then see why it is like that.

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

Sidebar

Related Questions

I have posted a question about a similar issue before, and managed to fix
I have posted a question about a similar issue before, and managed to fix
OK I'm a beginner and I have to fix this java expression language code
I have managed to break a working application, and cannot work out how to
I have managed to extract the filename and the extension and passed that to
I have managed to confuse myself whether I should return E_NOTIMPL or E_NOINTERFACE from
I have managed to set up Cassandra + Thrift and the Python wrapper for
I have managed to get a cron job to run a rake task by
I have managed to create a custom action in C# using MakeSfxCA which is
I have managed to bring the following output with this query SELECT DISTINCT IF(purchaseproduct_customerid=0,contactperson,vendorname)

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.