-2

I am in a low-level java class where I need to make an app using java and JavaScript. I have decided to create a game that creates a random equation using addition, subtraction, multiplication, and division. the trouble I am having is that my equation is created in the form of a string and I need to be able to calculate the answer to compare it to the users answer. I thought I had a descent solution to my issue but I keep getting some issues. can you help? my code is below:

import java.util.*;
public class GameClient {

    private static Stack<String> operations;
    private static Stack<Integer> numbers;
    private static String[] tokens;
    private static int i;
    private static Stack<String> subAdd;
    private static Stack<Integer> restOfNumbers;

    public static void main(String[] args) {
        Random variable = new Random();
        int numberOfVariables = variable.nextInt(5)+2;      //determines length of equation
        String equation = "";
        equation = equationGenerator(equation, numberOfVariables, variable);
        System.out.println(equation);
        System.out.print(calculateAnswer(equation));

    }
    public static String equationGenerator(String equation, int numberOfVariables, Random variable){
        int operation;
        int var;

        var = variable.nextInt(10)+1;

        equation += var;
        operation = variable.nextInt(4)+1;

        if(numberOfVariables == 1){
            equation += " = ";
            return equation;
        }

        if(operation == 1)
        {
            equation += " + ";
        }
        else if(operation == 2)
        {
            equation += " - ";
        }
        else if(operation == 3)
        {
            equation += " / ";
        }
        else if(operation == 4)
        {
            equation += " * ";
        }

        return equationGenerator(equation, numberOfVariables-1, variable);
    }

    public static int calculateAnswer(String equation){
        String delims = "[ ]+";
        tokens = equation.split(delims);
        for(i=0; i< tokens.length; i++)                     //does all multiplication and division first leaving just addition and subtraction
            switch(tokens[i]){
            case "0": case "1":case "2":case "3":case "4":case "5":case "6":case "7":case "8":case "9":
                int number = Integer.parseInt(tokens[i]);
                popOrPush(number);
                break;
            case "*": case "/": case "+": case "-":
                popOrPush(tokens[i], tokens);
            }

        while(!numbers.empty()){                    //flips number and operation stacks to do addition and subtraction in correct order
            restOfNumbers.push(numbers.pop());
        }
        while(!operations.empty()){
            subAdd.push(operations.pop());
        }
        while(!subAdd.empty()){
            switch(subAdd.pop()){
            case "+":
                restOfNumbers.push(restOfNumbers.pop() + restOfNumbers.pop());
            case "-":
                restOfNumbers.push(restOfNumbers.pop() - restOfNumbers.pop());
            }
        }
        return restOfNumbers.pop();
    }

    public static void popOrPush(int number){
        numbers.push(number);
    }
    public static void popOrPush(String operation, String[] tokens){
        switch(operation){
        case "*": 
            int multipliedValue = numbers.pop();
            i++;
            multipliedValue = multipliedValue * Integer.parseInt(tokens[i]);
            numbers.push(multipliedValue);
        case "/": 
            int dividedValue = numbers.pop();
            i++;
            dividedValue = dividedValue / Integer.parseInt(tokens[i]);
            numbers.push(dividedValue);
        case "+": case "-":
            operations.push(operation);
    }
    }
}
Kohei TAMURA
  • 4,970
  • 7
  • 25
  • 49
boydf
  • 7
  • 3
  • So, is it java or javascript? Cause I have news for you, it can't be both. (Why do you add a tag when don't know what it means?) – Mike Nakis May 07 '17 at 20:47
  • If it's Java, have a look at [this](http://stackoverflow.com/questions/3422673/evaluating-a-math-expression-given-in-string-form) SO answer. – Darshan Mehta May 07 '17 at 20:51
  • This is Java not Javascript. –  May 07 '17 at 21:00
  • ok cool yes its java. thats not the question though. sorry I don't know exactly what I'm talking about, hence why I'm on this site. does anyone see why I am getting a NullPointerException when trying to run this? its gotta be in the calculateAnswer method or popOrPush method because it will create the equation without issue. – boydf May 07 '17 at 21:14
  • 1
    "I keep getting some issues" is an utterly useless problem description. If you have an error, post the stack trace, describe what you've done to solve it and explain where specifically you're stuck. – shmosel Aug 04 '17 at 04:35

1 Answers1

0

You're not initializing your Stack, so you're likely getting a NPE when trying to use the numbers variable.

Make sure to initialize all of your variables (particularly your Stack objects). For example:

Stack<Integer> numbers = new Stack<>();
Rohan
  • 541
  • 1
  • 11
  • 24