Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Adding Big Numbers

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Adding Big Numbers

    How do you add a number of any length in Java which are strings without BigInteger?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Adding Big Numbers

    The same way you add numbers by hand: starting from right to left, take a digit, then add it to a number in the same digit location. If you need to carry, put a carry for the next number. Repeat until there are no more digits left.

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Adding Big Numbers

    Would you have any idea how to put that into code at all?

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Adding Big Numbers

    I won't do all the work for you, but I will give you some hints.

    You can get a character from a String using the charAt() method. This will give you the character at a specific index. Note that the index 0 is the furthest left number, and as with Java array indices the last index is String.length() - 1.

    Once you have the character, it will likely be a unicode value representing '0'..'9'. The actual values are different than 0..9 (notice the subtle difference). Before you perform the addition, you must subtract off the offset for '0' (this works because'0'..'9' are all consecutive codes and in increasing order). Before writing to your output string you must first determine if you need to carry (if the result came out to be greater than 10), and then subtract that off before re-adding the offset '0'.

  5. The Following User Says Thank You to helloworld922 For This Useful Post:

    hoiberg (December 16th, 2010)

  6. #5
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Adding Big Numbers

    Well I guess my question is how would I implement it. Like the carrying the 1 would have to be in an if statement. Also what kind of loop would the Char.at be in?

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Adding Big Numbers

    Did you not like the identical answer you got in your other post?

    http://www.javaprogrammingforums.com...ssignment.html

Similar Threads

  1. Java program to do Matrix operation
    By saladfingers73 in forum Collections and Generics
    Replies: 5
    Last Post: March 7th, 2012, 09:17 AM
  2. Adding to Array from JavaSpace problem
    By rtumatt in forum Collections and Generics
    Replies: 2
    Last Post: September 15th, 2011, 07:11 AM
  3. adding up odd and even numbers
    By darlinho in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 30th, 2010, 03:28 PM
  4. [SOLVED] What is not right here? adding JPanel in JFrame
    By Asido in forum AWT / Java Swing
    Replies: 2
    Last Post: August 23rd, 2010, 08:16 AM
  5. [SOLVED] Problem with a tutorial program(Adding the answer of two squared numbers together)
    By Melawe in forum What's Wrong With My Code?
    Replies: 20
    Last Post: April 7th, 2010, 09:03 AM