• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sum of big integers through chars  RSS feed

 
Bruno Ribeiro
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Since can't hold values greater than 2,147,483,647. On this assignment I have to create a method that will accept as input two numbers represented as Strings. It should then return a String representing the sum of the two input numbers. To compute this sum, the computer to do arithmetic the same way that we learned in grade school: work from right to left, summing the digits of each string, and keeping track of the carry across columns.

I tried this, but it doesn't print the final value. I have redone it over and over again and I still can't find what is wrong with it:



Thanks for the help!
Bruno
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bruno Ribeiro wrote:
I tried this, but it doesn't print the final value. I have redone it over and over again and I still can't find what is wrong with it:


Does the exception give you a hint?

Henry
 
Bruno Ribeiro
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean using try and catch?

Bruno
 
Knute Snortum
Sheriff
Posts: 4275
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your for loops are wrong. Can you see why?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bruno Ribeiro wrote:Do you mean using try and catch?


By default, when you don't use try-catch, any uncaught exceptions gets printed to standard out. And in general, this exception printout is very good at helping with figuring out the issue.

Anyway, it looks like you are using some sort of framework, probably written by your instructor, that catches and discards any uncaught exceptions. This is not good... perhaps you should catch and print out the exceptions (before your framework catches and discard them).

Henry
 
Bruno Ribeiro
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Your for loops are wrong. Can you see why?


Can't see why exactly. I reversed them so I could start for the last number, sum it, and then jump to the next one.

Henry Wong wrote:By default, when you don't use try-catch, any uncaught exceptions gets printed to standard out. And in general, this exception printout is very good at helping with figuring out the issue.


This may sound like a dumb question, but do I include the for loops in the exception, or outside of it?

 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bruno Ribeiro wrote: . . .
Can't see why exactly. I reversed them so I could start for the last number, . . .
There is a standard form for traversing an array with a for loop, and a standard form for traversing it backwards. I would appear to have written about it here. See if that doesn't give you a hint about your loop.

[edit: addition]Depends where the Exception is likely to occur. If you think there are problems with a particular method, wrap its entire contents in try‑catch.
 
Bruno Ribeiro
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I followed you advice on the loops, and it sums with small numbers, but once the values are higher than 10 everything goes wrong.
So, my loops are wrong somehow, I can't have two loops (one inside another) because it does 4 sums. For example: 10 + 10 = 0112

Finnaly my question is, how do you suggest I get all the values from the characters but separately (n1 and n2)?

Code:

 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to look at your loops closer. Think through your intent in implementing nested loops.
You could put in some print statements to see what the loops are doing.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

And after the OP examines and fix the loops, the OP should take a look at the carry over mechanism. Not only is the carry over calculation incorrect, and its usage is incorrect, but it actually doesn't carry over to the next iteration of the loops (which of course, is currently incorrect).

Perhaps it would be a good idea to work it out with pen and paper first.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!