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;
}