SenjuXo
SenjuXo

Reputation: 155

Stack Behavior with Math

I am adding two very large Integers, up to 125 digits, without the use of the Integer class or the BigInteger class, only java's Stack utility. It just simply loads the two large integers into a stack and then compares each pop().

I initially have a method to load the stacks, A and B from their own JTextArea.getText()

 public Stack<Integer> loadStack(String numA)
    {
        Scanner scan = new Scanner(numA);
        Stack<Integer> stack = new Stack<Integer>();
        while (scan.hasNext())
        {
            stack.push(scan.nextInt());
        }
        //System.out.println(stack.toString());
        return stack;
    }

and then my method that displays the resulting stack is called resTF.setText(num.addStacks(stackA, stackB).toString()); where resTF is another JTextArea for the result.

my method that adds takes two Stack<Integer>'s

public Stack<Integer> addStacks(Stack<Integer> stackA, Stack<Integer> stackB)
    { 
        Stack<Integer> resultStack = new Stack<Integer>();

        while(!stackA.empty() && !stackB.empty())
        {
            try
            {   
                int carry = 0;
                //get the digits to add
                int tokenA = stackA.pop();
                int tokenB = stackB.pop();

                //add them and mod 10
                int result = tokenA + tokenB + carry;
                int resultDigit = result % 10;

                //push the result on to the new stack
                resultStack.push(resultDigit);

                //the updated carry
                carry = result / 10;
                if (carry > 0)
                {
                    resultStack.push(carry);
                }
            }
            catch(ArithmeticException e)
            {
                e.printStackTrace();
            }
        }
        System.out.println(resultStack.toString());
        return resultStack;
    }

1: My stack is giving me output such as, [6, 66] when adding 555 and 111 when the desired output would be [6,6,6] i think? Why is this? because of the way its being read in? I believe i am messing up possibly in the addition.

2: When i type in very very very large numbers like 100000000000000000000000000000000000000 and 200000000000000000000000000000000000000 I get, so I know its my loadStacks method that is cause the issues, particularly the scanning it in. What am i missing?

Exception in thread "AWT-EventQueue-0" java.util.InputMismatchException: For input string: "100000000000000000000000000000000000000"
    at java.util.Scanner.nextInt(Scanner.java:2123)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at GUI.BigNumber.loadStack(BigNumber.java:19)

EDIT 1*****

public void checkJagged(Stack<Integer> stackA, Stack<Integer> stackB)
    {
        int stackSizeA = stackA.size();
        int stackSizeB = stackB.size();

        if (stackA.size() < stackB.size())
        {
            for (int i = 0; i < stackSizeB; ++i)
            {
                if (stackA.elementAt(i) == null)
                {
                    stackA.push(0);
                }
            }
        }
        if (stackA.size() > stackB.size())
        {
            for (int i = 0; i < stackSizeA; ++i)
            {
                if (stackB.elementAt(i) == null)
                {
                    stackB.push(0);
                }
            }
        }
    }

Upvotes: 1

Views: 132

Answers (2)

thst
thst

Reputation: 4602

I think your problem is that you expect nextInt()to return only one digit, but it does return all consecutive digits.

You need to consume the textbox contents as String and work on the characters.

 public Stack<Integer> loadStack(String numA)
 {
     if(numA == null) throw new IllegalArgumentException("...");
     char[] chars = numA.toCharArray();
     Stack<Integer> stack = new Stack<>();
     for (char c : chars) {
        if (Character.isDigit(c))
            stack.push((c - '1') < 9 ? (c - '1' + 1) : 0);
     }
     return stack;
 }

Upvotes: 0

Stefan Haustein
Stefan Haustein

Reputation: 18803

Input processing is causing part of the described problem -- the scanner will read the whole number as one value. Do something like

for (int i = 0; i < numA.length(); i++) {
  stack.push(Integer.parseInt(numA.substring(i, i + 1));
} 

The other problem is that you push the carry in the loop. This would result in 1 2 1 2 1 2 for 666 + 666 with a fixed parser. Its' sufficient to add the carry in the loop, and only push the final carry value after the loop. Also, set it to 0 before the loop, so the previous carry is actually added (opposed to be overwritten with 0).

Moreover, you'll need to account for the case where the stacks are of different sizes. Simplest way is to keep going while one stack is not empty, and treat the exhausted stack as containing zeros.

Upvotes: 2

Related Questions