1

When I try to use the function to iterate the user input expression, I get the java.lang.NumberFormatException, I try fixing the loop much time, but I still cannot understand where did it when wrong. The IDE suggest it went wrong in the parstInt while loop

Here is the code:

import java.util.Scanner;
import java.util.Stack;

static Stack<Integer> stackForOperand = new Stack<Integer>();
static Stack<Character> stackForOperator = new Stack<Character>();

public static int processOneOperator(char stackForOperator, int num1, int num2) {

    int result = 0;

    switch (stackForOperator) {

    case '+':
        result = num1 + num2;
    case '-':
        result = num1 - num2;
    case '*':
        result = num1 * num2;
    case '/':
        if (num2 != 0) {
            result = num1 / num2;
        } else {
            throw new UnsupportedOperationException("divide by zero error");
        }
    }
    return result;
}

public static boolean num_order(char first, char second) {

    if (first == '(' || second == ')') {
        return false;
    } else if ((first == '*' || first == '/') && (second == '+' || second == '-')) {
        return false;
    } else {
        return true;
    }
}

public static int calculation_loop(String expression) {

    for (int i = 0; i < expression.length(); i++) {

        if (expression.charAt(i) >= '0' && expression.charAt(i) <= '9') {
            String more_num = "";

            while (i < expression.length() && expression.charAt(i) >= '0' && expression.charAt(i) <= '9') {
                more_num += expression.charAt(i++);
                int more_num2 = Integer.parseInt(more_num);
                stackForOperand.push(more_num2);
                i--;
            }

        } else if (expression.charAt(i) == '(') {
            stackForOperator.push(expression.charAt(i));

        } else if (expression.charAt(i) == ')') {

            while (stackForOperator.peek() != '(') {
                stackForOperand.push(
                        processOneOperator(stackForOperator.pop(), stackForOperand.pop(), stackForOperand.pop()));
                stackForOperator.pop();
            }

        } else if (expression.charAt(i) == '+' || expression.charAt(i) == '-' || expression.charAt(i) == '*'
                || expression.charAt(i) == '/') {

            while (!stackForOperator.empty() && num_order(expression.charAt(i), stackForOperator.peek())) {
                stackForOperand.push(
                        processOneOperator(stackForOperator.pop(), stackForOperand.pop(), stackForOperand.pop()));
                stackForOperator.push(expression.charAt(i));
            }
        }
    }
    while (!stackForOperator.empty()) {
        stackForOperand
                .push(processOneOperator(stackForOperator.pop(), stackForOperand.pop(), stackForOperand.pop()));
    }
    return stackForOperand.pop();
}

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    System.out.println("/");
    String input = scanner.nextLine();
    input = input.replaceAll("\\s+", "");

    System.out.println(input);
    Integer output = calculation_loop(input);

    System.out.println(output);

}

}

10 Rep
  • 2,217
  • 7
  • 19
  • 33
HyQ
  • 21
  • 2
  • 2
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Arvind Kumar Avinash Oct 12 '20 at 21:48
  • 1
    You need to include the complete error message and stack trace, plus the input you are giving the program. – tgdavies Oct 12 '20 at 23:40
  • I would recommend reading https://ericlippert.com/2014/03/05/how-to-debug-small-programs/. – AMC Oct 13 '20 at 01:47
  • Kindly explain what you expect his program to do. Please see [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Scratte Oct 13 '20 at 16:45

1 Answers1

1

Looking at this piece of code:

    public static int calculation_loop(String expression) {

        for (int i = 0; i < expression.length(); i++) {

            if (expression.charAt(i) >= '0' && expression.charAt(i) <= '9') {
                String more_num = "";

                while (i < expression.length() && expression.charAt(i) >= '0' && expression.charAt(i) <= '9') {
                    more_num += expression.charAt(i++);
                    int more_num2 = Integer.parseInt(more_num);
                    stackForOperand.push(more_num2);
                    i--;
                }

So. Suppose having such an expression "2345+6789". According to your code i is incrementing and decrementing before end of while loop.

              while (i < expression.length() && expression.charAt(i) >= '0' && expression.charAt(i) <= '9') {
                    more_num += expression.charAt(i++);
                    ...
                    i--;
                }

Is this your intention? It makes your loop infinite and it finishes only because number format exception is thrown while parsing. This is why I think your parser throws exception: You got the digit '2' in first place, concatenate it with more_num, then increment the i, after that decrement the i and at the next iteration you have the same previous position with same char '2', then concatenate it again to more_num ad infinitum. And first you parse more_num which is "2", on next iteration you append one more '2' and more_num "22" then "222"... Till it become bigger than type int can hold like "22222222222222222222222" and exception is thrown
Second thing. Suppose you remove i-- and your loop will normally iterate the next char. So, your stackForOperand will push first the number 2, then will push the number 23 then the number 234, then will push the number 2345. Is that your intension? I think more logically is to move Integer.parseInt and stackForOperand.push after the while loop

    public static int calculation_loop(String expression) {

        for (int i = 0; i < expression.length(); i++) {

            if (expression.charAt(i) >= '0' && expression.charAt(i) <= '9') {
                String more_num = "";

                while (i < expression.length() && expression.charAt(i) >= '0' && expression.charAt(i) <= '9') {
                    more_num += expression.charAt(i++);
                }
                int more_num2 = Integer.parseInt(more_num);
                stackForOperand.push(more_num2);
armagedescu
  • 1,758
  • 2
  • 20
  • 31