programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# This far on assignment

Pat Steele
Greenhorn
Posts: 22
I have to seperate a number 9876 to 6 7 8 9, to 9 8 7 6. I need to know how to sum the digits. I have gotten to this point ::

To this point its gives me the seperate integers. OK but how do I get the variable integers into seperate containers and then add them up?

My assignment is to do that, and what I have above gets me to where I have seperate digits, but how do I catch them into seperate entities to be added to a sum?

Liutauras Vilda
Sheriff
Posts: 4928
334
Pat Steele wrote:I have to seperate a number 9876 to 6 7 8 9, to 9 8 7 6. I need to know how to sum the digits.

Hi Pat,

Please clarify, what exactly you have to do?

Liutauras Vilda
Sheriff
Posts: 4928
334
Anyway, this is the way how to get separate digits, then you can sum them, reverse..

int thousands = (n / 1000);
int hundreds = (n / 1000) / 100;
int tens = (n / 1000) % 100 / 10;
int units = (n / 1000) % 100 % 10;

Try to work out and post here your further solution.

Campbell Ritchie
Marshal
Posts: 56599
172
You can try working out the length of the number and create an array of that length.

If you have the number as a String you can turn it into an int with this Integer class method, and you can get an array of chars with a method of the String, but the value of the char '0' isn't 0 but 0x30 (=48).

You can turn the number into a String with this String class method and get the length of the String easily then you will know what size array to create.

There are no doubt other ways to do it.

Vitaliy Gaydarenko
Greenhorn
Posts: 22
I am agree with Liutauras Vilda, i am not understand question too.
You have a sequence of numbers, and what do you want to do with them? parse by one symbol? sorted it? And take sum of them?Or what?

Campbell Ritchie
Marshal
Posts: 56599
172
Liutauras Vilda wrote: . . .
int thousands = (n / 1000);
int hundreds = (n / 1000) / 100;
int tens = (n / 1000) % 100 / 10;
int units = (n / 1000) % 100 % 10;
. . .
What was wrong with the way OP was calculating the individual digits? I prefer that to your method, which has repeated code and lots of magic numbers in.

Campbell Ritchie
Marshal
Posts: 56599
172
You will also get 0 for hundreds; I didn't notice the error earlier.

Liutauras Vilda
Sheriff
Posts: 4928
334
My mistake, hundreds should be (n % 1000) / 100
Thank you for spotting that.

The way he did, i understood that he is unable to store single numbers into separate variables, didn't understand fully what he meant: "OK but how do I get the variable integers into seperate containers and then add them up?"

Tony Docherty
Bartender
Posts: 3271
82
I agree with Campbell that the OP's current approach is the probably best way to tackle this problem.

Liutauras Vilda wrote:The way he did, i understood that he is unable to store single numbers into separate variables, didn't understand fully what he meant: "OK but how do I get the variable integers into seperate containers and then add them up?"

It's certainly not clear what the problem is but I suspect the OP needs to put each individual number in an array and then sum the digits. ie 6789 -> 6, 7, 8, 9 -> 30
Of course you could more easily do the summing on the fly and this may in fact be all that is required and the storage part is just the OP's idea of how to get to the final result - we won't know until the OP confirms the exact problem.

Pat Steele
Greenhorn
Posts: 22
I need the sum of the digits, so in my first post, I get the "4567" into 4 5 6 7 . Then I need to get the sum of the digits.

Campbell Ritchie
Marshal
Posts: 56599
172
You get the sum of the digits by adding them together. Hint: start like this:-

Rico Felix
Ranch Hand
Posts: 411
5
If you take into consideration that everything within a digital computing system is stored as a sequence of bytes, then performing any computation becomes a simple matter of seeing the conversions from machine notation to human notation and vice versa...

Luckily within an environment such as Java we have libraries to do all the conversions for us...

Therefore you can simply accept the number as a string, break the string apart by each character to acquire each digit, transform each character to an integer, then perform the addition on the integers...

Campbell Ritchie
Marshal
Posts: 56599
172
But OP already has the number as a number and already knows how to get the individual digits.

Liutauras Vilda
Sheriff
Posts: 4928
334
Campbell Ritchie wrote:But OP already has the number as a number and already knows how to get the individual digits.

So, the problem should be sorted by now then.

Pat, what final code did you get?

 Don't get me started about those stupid light bulbs.