• Post Reply Bookmark Topic Watch Topic
  • New Topic

This far on assignment  RSS feed

 
Pat Steele
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
IntelliJ IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will also get 0 for hundreds; I didn't notice the error earlier.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You get the sum of the digits by adding them together. Hint: start like this:-
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But OP already has the number as a number and already knows how to get the individual digits.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!