Pat Steele

Greenhorn

Posts: 22

posted 2 years ago

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?

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?

Campbell Ritchie

Marshal

Posts: 56599

172

posted 2 years ago

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

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.

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`char`s 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.

Campbell Ritchie

Marshal

Posts: 56599

172

posted 2 years ago

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.Liutauras Vilda wrote: . . .

int thousands = (n / 1000);

int hundreds = (n / 1000) / 100;

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

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

. . .

Campbell Ritchie

Marshal

Posts: 56599

172

posted 2 years ago

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?"

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

posted 2 years ago

I agree with Campbell that the OP's current approach is the probably best way to tackle this problem.

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.

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

Campbell Ritchie

Marshal

Posts: 56599

172

posted 2 years ago

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...

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

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