My big integer calculator is not working as planned, can anyone java developers help me out?

Viewed 71

I am making a big integer calculator for my comp sci class but some of the things I add together just don't work. Basically to my knowledge it only works with numbers 100 and below (maybe also 999 and below). Specifically what I'm struggling on is that when adding 934 + 168 it always comes out to 10102, instead of 1102. I have made this add up together before, but in the process of trying to fix other problems it doesnt work anymore. If anyone would be able to help me fix my code by telling me where I may have gone wrong, please do. Thanks!

public static void main(String[] args) {
    System.out.println("Please enter a positive integer.");
    LinkedList a = takeData();
    System.out.println("Please enter another positive integer.");
    LinkedList b = takeData();
    LinkedList<Integer> sum = add(a, b);
    //System.out.println("They add to equal:\t");
    print(sum);
}


public static LinkedList<Integer> add(LinkedList<Integer> a, LinkedList<Integer> b) {
    LinkedList<Integer> sum = new LinkedList<Integer>();
    int decide = 0, s = 0;
    ;
    if (a.size() == b.size())
        decide = 1;
    fillWithZeros(a, b, sum);
    print(a);
    print(b);
    if (decide == 1)
        s = a.size() + 1;
    else
        s = a.size();
    int add = 0, count = 0;

    for (int x = 0; x < s; x++) {
        count = 0;
        add = 0;
        if (x != s && decide == 0 || decide == 1 && x != s - 1)
            add = a.pollLast() + b.pollLast();
        if (sum.size() != 0)
            add += sum.getFirst();
        if (add >= 10) {
            add -= 10;
            //if(sum.size()>0)
            //add+=sum.get(0);
            sum.addFirst(0);
            sum.addFirst(1);
            count++;
            //if(sum.size()>=2) THIS DOES NOTHING
            //add+= sum.get(1);
        }
        System.out.println("\nThe " + x + " digit equals = " + add);
        System.out.println("x = " + x + "\nsum.size = " + sum.size());
           /* if(sum.size()>s-1 && decide==1)
            {
                System.out.println("YOu stopperd here");
            return sum;
        }*/
        if (x != 0) {
            if (sum.size() >= x) {
                add += sum.get(1);


                if (add >= 10) {
                    add -= 10;
                    sum.addFirst(0);
                    sum.addFirst(1);
                    if (sum.size() >= 2)
                        add += sum.get(1);
                    //add-=10;
                    //sum.addFirst(1);
                    sum.set(1, add);
                } else {
                    sum.set(1, add);
                }

                System.out.println(add);

                //sum.set(0,add);
                //}
                //sum.add(x-1,add);
                //sum.add(add);
            } else {
                //add+= sum.get(x-1);
                if (count == 0)
                    sum.set(0, add);
                else
                    sum.set(1, add);
            }
        } else
            sum.addLast(add);
        print(sum);
        if (sum.size() > s - 1 && decide == 1)
            return sum;

    }


    return sum;
}

public static void print(LinkedList<Integer> a) {
    System.out.println("\nSize of LinkList:\t" + a.size());
    for (int x = 1; x <= a.size(); x++)
        System.out.print(a.get(x - 1));
}

public static void fillWithZeros(LinkedList<Integer> a, LinkedList<Integer> b, LinkedList<Integer> c) {
    while (b.size() != a.size()/*||c.size()!=b.size()*/) {
        if (a.size() > b.size())
            b.addFirst(0);
        else if (a.size() < b.size())
            a.addFirst(0);
        /*else
            c.addFirst(0);*/
    }
    /*c.addFirst(0);*/
}

public static LinkedList<Integer> takeData() {
    boolean correctData = true;
    Scanner read = new Scanner(System.in);
    LinkedList list = new LinkedList();
    do {
        if (correctData == false)
            System.out.println("Please re-enter a correct integer.");
        String n = read.nextLine();
        for (int x = 0; x < n.length(); x++) {
            if (Character.isDigit(n.charAt(x))) {

                int valueOf = n.charAt(x) - 48;
                list.add(valueOf);

            } else {
                correctData = false;
                break;
            }
            correctData = true;
        }

    } while (correctData == false);

    return list;
}
1 Answers

You should add the numbers the same way you'd do it on paper, i.e. start with the last digits, add them, and carry over a 1 if needed.

To iterate through a list using an Iterator, you probably already know you can do it like this:

Iterator<Integer> iter = list.iterator();
while (iter.hasNext()) {
    int digit = iter.next();
    // use digit here
}

A ListIterator can do the same, but it can also iterate backwards:

ListIterator<Integer> iter = list.listIterator(list.size()); // start at end
while (iter.hasPrevious()) {
    int digit = iter.previous(); // iterate backwards
    // use digit here
}

The LinkedList class is excellent for building the result backwards too, because in addition to add(), it also has addLast() (which does the same thing), as well as addFirst(), which we can use to build result starting with the last digit.

Knowing all that then makes it quite simple, something like this:

public static LinkedList<Integer> add(LinkedList<Integer> a, LinkedList<Integer> b) {
    LinkedList<Integer> sum = new LinkedList<>();
    int carry = 0;
    ListIterator<Integer> aIter = a.listIterator(a.size());
    ListIterator<Integer> bIter = b.listIterator(b.size());
    while (carry != 0 || aIter.hasPrevious() || bIter.hasPrevious()) {
        int digitSum = carry;
        if (aIter.hasPrevious())
            digitSum += aIter.previous();
        if (bIter.hasPrevious())
            digitSum += bIter.previous();
        if (digitSum <= 9) {
            carry = 0;
        } else {
            carry = 1;
            digitSum -= 10;
        }
        sum.addFirst(digitSum);
    }
    return sum;
}
Related