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 5 of 5

Thread: Adding the integers of a number

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Adding the integers of a number

    I really need a hand, I have to write the java for the following; the user enters a number and the integers of that number are added, for example I enter "234" and the programs produces "2+3+4=9"

    Really grateful for any help received.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Adding the integers of a number

    Look at using these two operators: / and %
    One to get a digit, the other to shift the remaining digits to the right.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding the integers of a number

    Cheers,
    Would you have any idea how it would look??

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Adding the integers of a number

    Try experimenting with some simple statements that use those operators and print out the results.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2010
    Posts
    40
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default Re: Adding the integers of a number

    does it print the result as "2+3+4=9" or it will add all the numbers producing 9 as a result?

    i will assume you are looking for the former output. first you have to separate all numbers and add them to an array, for this you may use the toCharArray() method of String class. After this create a loop that will traverse your array, convert them into integer since your array is in char datatype. Then add them to variable and print the result.

Similar Threads

  1. Adding integers from a file
    By LoganC in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 1st, 2012, 04:10 PM
  2. Reading text file and counting the number of words, integers, and floats.
    By Jsmooth in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: April 12th, 2012, 06:39 PM
  3. [SOLVED] Reading integers from a file, but it's only displaying the first number repeatedly..?
    By jessica202 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 29th, 2011, 08:16 AM
  4. [SOLVED] Writing Integers to .txt File; Returning Random Characters Instead of Integers
    By verbicidalmaniac in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 8th, 2011, 09:42 PM
  5. Determine the two smallest integers from a set of user input integers
    By bpontin in forum Loops & Control Statements
    Replies: 4
    Last Post: October 17th, 2010, 06:38 PM