0

I am currently working on code that is intended to parse a string into a linked list, and use that list as a means to compute an operation. As seen, I have the operation string currently set to "1 + 2", and when run, it returns the integer 0.

All help is greatly appreciated.

import java.awt.Dimension;
import java.util.LinkedList;
import java.util.List;


public class LinkedListCalculator {

static List<String> CalcInput = new LinkedList<String>();
static String operation = "1+2";
static int equationLength = operation.length();
static int result = 0;
static int num = 0;
static int mul = 1;
static int finalNum = 0;
static char lastOperator = 0;


public static void addToList() {
    String finalNumString;
    if (lastOperator == '+') {
        finalNumString = Integer.toString(finalNum);
        CalcInput.add(finalNumString);
        CalcInput.add("+");
    } else if (lastOperator == '-') {
        finalNumString = Integer.toString(finalNum);
        CalcInput.add(finalNumString);
        CalcInput.add("-");
    } else if (lastOperator == '*') {
        finalNumString = Integer.toString(finalNum);
        CalcInput.add(finalNumString);
        CalcInput.add("*");
    } else if (lastOperator == '/') {
        finalNumString = Integer.toString(finalNum);
        CalcInput.add(finalNumString);
        CalcInput.add("/");
    }
}

public static void computeList() {
    int size = CalcInput.size();
    String lastIndex = null;
    for (int i = 0; i > size; i++) {
        String currentIndex = CalcInput.get(i);
            if (i > 0) {
                lastIndex = CalcInput.get(i - 1);
            }

            if (currentIndex == "+") {
                int lastIndexInt = Integer.valueOf(lastIndex);
                result += lastIndexInt;
            }
            if (currentIndex == "-") {
                int lastIndexInt = Integer.valueOf(lastIndex);
                result -= lastIndexInt;
            }
            if (currentIndex == "*") {
                int lastIndexInt = Integer.valueOf(lastIndex);
                result *= lastIndexInt;
            }
            if (currentIndex == "/") {
                int lastIndexInt = Integer.valueOf(lastIndex);
                result /= lastIndexInt;
            }
    }

}

public static void main(String [] args) {
    for (int currEqnCharNdx = 0; currEqnCharNdx < equationLength ; currEqnCharNdx++) {
        char j = operation.charAt(currEqnCharNdx);

        if (j >= '0' && j <= '9') {
            num = j - '0';
            finalNum *= mul;
            finalNum += num;
            mul = 10;
        } else if (operation.charAt(currEqnCharNdx) == '+') {
            addToList();
            lastOperator = '+';
            mul = 1;
            finalNum = 0;
        } else if (operation.charAt(currEqnCharNdx) == '-') {
            addToList();
            lastOperator = '-';
            mul = 1;
            finalNum = 0;
        } else if (operation.charAt(currEqnCharNdx) == '*') {
            addToList();
            lastOperator = '*';
            mul = 1;
            finalNum = 0;
        } else if (operation.charAt(currEqnCharNdx) == '/') {
            addToList();
            lastOperator = '/';
            mul = 1;
            finalNum = 0;
        } 
    }
    int i = CalcInput.size();
    computeList();
    System.out.println("result: " + result);
}
}
AstroCB
  • 12,337
  • 20
  • 57
  • 73
Bandit Bat
  • 25
  • 1
  • 8
  • Please provide more information about what goes wrong. Have you tried stepping through the code with a debugger? If this is not a pet project use a real expression parser, as described in http://stackoverflow.com/questions/1432245/java-parse-a-mathematical-expression-given-as-a-string-and-return-a-number – ooxi Oct 28 '13 at 15:38
  • Questions about specific code snippets should have a stack trace if they throw an exception. If there is a logical error (wrong result), there should be reasons explaining why the author wasn't able to catch it in a debugger. Please add more information. Thanks! – Giovanni Botta Oct 28 '13 at 15:40

1 Answers1

0

There is alot wrong with your code. Here are some pointers to errors:

String lastIndex = null;
    for (int i = 0; i > size; i++) {

this will never execute (change to i < size)

Also i suspect that addToList() should happen AFTER lastOperator is set, etc.

 else if (operation.charAt(currEqnCharNdx) == '+') {
                addToList();
                lastOperator = '+';
                mul = 1;
                finalNum = 0;
            }

Also, here (in addToList()):

if (lastOperator == '+') {
        finalNumString = Integer.toString(finalNum);
        CalcInput.add(finalNumString);
        CalcInput.add("+");
    }

You add the last number, plus the + sign, but then nothing ever causes the 2 to be added, so when it sums up at the end (if you fix the other things i mentioned) it will sum 1+ making a result of 1 (rather than 1+2 making 3).

Here is code of it outputting 1 (see last point above) for OP to reproduce:

import java.awt.Dimension;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;


public class LinkedListCalculator {

static List<String> CalcInput = new LinkedList<String>();
static String operation = "1+2";
static int equationLength = operation.length();
static int result = 0;
static int num = 0;
static int mul = 1;
static int finalNum = 0;
static char lastOperator = 0;


public static void addToList() {
    String finalNumString;
    if (lastOperator == '+') {
        finalNumString = Integer.toString(finalNum);
        CalcInput.add(finalNumString);
        CalcInput.add("+");
    } else if (lastOperator == '-') {
        finalNumString = Integer.toString(finalNum);
        CalcInput.add(finalNumString);
        CalcInput.add("-");
    } else if (lastOperator == '*') {
        finalNumString = Integer.toString(finalNum);
        CalcInput.add(finalNumString);
        CalcInput.add("*");
    } else if (lastOperator == '/') {
        finalNumString = Integer.toString(finalNum);
        CalcInput.add(finalNumString);
        CalcInput.add("/");
    }
}

public static void computeList() {
    int size = CalcInput.size();
    String lastIndex = null;
    for (int i = 0; i < size; i++) {
        String currentIndex = CalcInput.get(i);
            if (i > 0) {
                lastIndex = CalcInput.get(i - 1);
            }

            if (currentIndex == "+") {
                int lastIndexInt = Integer.valueOf(lastIndex);
                result += lastIndexInt;
            }
            if (currentIndex == "-") {
                int lastIndexInt = Integer.valueOf(lastIndex);
                result -= lastIndexInt;
            }
            if (currentIndex == "*") {
                int lastIndexInt = Integer.valueOf(lastIndex);
                result *= lastIndexInt;
            }
            if (currentIndex == "/") {
                int lastIndexInt = Integer.valueOf(lastIndex);
                result /= lastIndexInt;
            }
    }

}

public static void main(String [] args) {



    for (int currEqnCharNdx = 0; currEqnCharNdx < equationLength ; currEqnCharNdx++) {
        char j = operation.charAt(currEqnCharNdx);

        if (j >= '0' && j <= '9') {
            num = j - '0';
            finalNum *= mul;
            finalNum += num;
            mul = 10;
        } else if (operation.charAt(currEqnCharNdx) == '+') {

            lastOperator = '+';
            addToList();
            mul = 1;
            finalNum = 0;
        } else if (operation.charAt(currEqnCharNdx) == '-') {
            addToList();
            lastOperator = '-';
            mul = 1;
            finalNum = 0;
        } else if (operation.charAt(currEqnCharNdx) == '*') {
            addToList();
            lastOperator = '*';
            mul = 1;
            finalNum = 0;
        } else if (operation.charAt(currEqnCharNdx) == '/') {
            addToList();
            lastOperator = '/';
            mul = 1;
            finalNum = 0;
        } 
    }
    int i = CalcInput.size();
    computeList();
    System.out.println("result: " + result);
}
}
Aaron
  • 652
  • 4
  • 10
  • Oh dear, I have no idea how I didn't catch those, thanks! However, it still returns 0. Any pointers? – Bandit Bat Oct 28 '13 at 15:56
  • If you flip the > i mentioned, and move addToList() down under the lastOperator = ? line, then you should get a result of 1 (see my last edit above) – Aaron Oct 28 '13 at 15:58
  • It would appear that it still returns 0 after performing these changes. – Bandit Bat Oct 28 '13 at 16:21
  • I'll post the code (i tried it with just those 2 changes i think, resulting in 1) in my answer - hopefully it won't look too spammy. – Aaron Oct 28 '13 at 16:33
  • Added code to answer. Note i only made the partial fixes for +, not other operators. – Aaron Oct 28 '13 at 16:36
  • Oh, that makes sense! I figured out what I was doing wrong too. I was putting `addToList();` below the line that resets `finalNum`. Thanks for the help, I should be able to get it from here :) – Bandit Bat Oct 28 '13 at 16:51