-2

The task is asking me to insert 2 strings of a very huge number and sum them from right to left like the way we studied in primary school like:

23 + 28 = (2 + 2 + <1> (this is the left 1 >>> keep reading ))(3 + 8 (give the left <1> from 11 sum with the two front numbers)) = 51.

The algorithm is ok but when I try to do like (just default that the 2 number has the same length so I get lenA and not greater than 10 to make easier):

 int len = a.length();
 String result="";
 for(int i = len; i>0 ; i--) { //<- We only need it to loop how many times
      result = Integer.valueOf( a.charAt(len-1) ) + Integer.valueOf( b.charAt(len-1) ) + "" + result;//<<<because we sum them from right to left
      lenA--; lenB--;
 }

and that (<<<) line always give me the error. This is just one of a many ways I tried if it's wrong and you have a solution, please just guide me, sometimes i think too much and forgot a lot of small details :))

So the question here is how can i change it from a digit of String to Integer, calculate it and print out String again. But after I read the .charAt() info it said: "Returns the char value at the specified index." so the question maybe confuse between the first question and Convert from a digit from String use charAt -> become Char then convert Char to Integer to calculate and finally convert back to String so that I can + with String result.

I tried a lot of way but still can't solve it.

M. Prokhorov
  • 3,894
  • 25
  • 39
  • "that line always give me the error" => You forgot to tell us, what error and what line! – Seelenvirtuose Mar 30 '18 at 08:46
  • And also in primary school we learn to add from right to left aka: 3+8 = (1)1 then: 2+2+(1) = 5 that gives: 23 + 28=51 – Calaom Mar 30 '18 at 08:47
  • Also you don't look like you notice that thoses strings may not be the same size so you may add tenth with units – Calaom Mar 30 '18 at 08:51
  • 1
    @Seelenvirtuose he marked the line at the end, but you have to scroll right. – Thomas Böhm Mar 30 '18 at 08:54
  • @Calaom When looking at his code, he adds from right to left, but the description was missleading. – Thomas Böhm Mar 30 '18 at 08:55
  • @ThomasBöhm Yes, my bad, I should've read the code a bit more, but he still works only with same sized numbers or at least expect them – Calaom Mar 30 '18 at 08:58
  • I'm a little confused correct me if i'm wrong. Result is always a string without any calculations. So basically all you do is picking 2 numbers and add them next to each other from 2 different strings. Why do you convert to integer if you dont do a calculation? you are missing ( ). Also as @Calaom stated if you have 2 different length strings this is gone give you a error. – H.Mikhaeljan Mar 30 '18 at 09:02
  • That's what i'm wondering, i already convert one single digit from both to Integer then sum them so it would be Char, String or Integer? – Henderson Mar 30 '18 at 10:29
  • And i said ( just default 2 number has the same length for easy or easier) – Henderson Mar 30 '18 at 10:30
  • You may find [this other answer](https://stackoverflow.com/a/4968343/7470253) useful. `Integer.valueOf` would not work like you might expect, because there is no version of that method that would accept `char`, only `int`. – M. Prokhorov Mar 30 '18 at 10:33
  • still not working – Henderson Mar 30 '18 at 10:44

1 Answers1

0
int lena = a.length();
int lenb = b.length();
int inta[] = new int[lena];
int intb[] = new int[lenb];
int result[];
int carry = 0, maxLen = 0, tempResult;

if(lena >lenb)
   maxLen = lena + 1;
else
   maxLen = lenb + 1;

result = new int[maxLen];

for(int i = lena - 1; i>=0 ; i--) {
   inta[i] = Integer.valueOf( a.charAt(i) );
}

for(int i = lenb - 1; i>0 ; i--) {
   intb[i] = Integer.valueOf( b.charAt(i) );
}

for(int i = maxLen - 1; i >= 0; i--) {
   result[i] = 0;
}

for(int i = 1; i < maxLen - 1; i++) {
   tempResult = 0;
   if(lena > i)
       tempResult += inta[lena - i];
   if(lenb > i)
       tempResult += intb[lenb - i];
   result[maxSize - i] += tempResult % 10;
   result[maxSize - i - 1] = tempResult / 10;
}

String res = "";
for(int i = 0; i < maxLen; i++) {
   res += result[i];
}

System.out.println(res);

I think that would do what you want and will avoid most simple mistakes (couldn't try it, no acces to an IDE).

I actually separate the two string into their inside number and then add them into the result tab.

Then I put the result tab into a string that I print.

Calaom
  • 244
  • 8
  • 25
  • Thanks, but my teacher want me to use String only, he said the number may growth to some billion so i should use String and split it, calculate one by one. I'm not sure if array[] have the same capacity as String or not? – Henderson Mar 30 '18 at 10:23
  • @Henderson, their lengths are both within the range of `int`, so technically one array can store all characters in a `String` without any problems. I don't see a benefit in having the array to begin with, it only consumes valuable space. – M. Prokhorov Mar 30 '18 at 10:32
  • @Henderson I think I start to see what your teacher wants but it's still looking useless here ^^' Basically he wants you to use only Strings? So you have to create a function doing the two character treatment: You put two char as entry and you have to get out the result and the carry and so on. But I won't do it since it's actually just a big switch cases. – Calaom Mar 30 '18 at 11:26
  • Yeah you're right, it's the hardest task I've ever had. I haven't done it yet but thank you anyway, i'm appreciate it. – Henderson Mar 30 '18 at 14:15