• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding numbers once I have them listed as individual numbers  RSS feed

 
Erica Splitt
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on a code that will list numbers out individually (ie. user types in 7634 and result is 7 6 3 4) and then those individual numbers are supposed to get added together.
I have the first part up and running but cannot for the life of me figure out how to add the results together. Also would it be difficult to add in a JOptionPane at this point? Not required but I want to try to use it. Thank you for your help!


Here is what I have so far ....

 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

First, you keep too much logic in main method. Don't do that! There is a page that explains it Main is a pain.
And split your loginc into some methods that you can test separately.

Your remainder logic is way too complicated. Why using Math.pow()?

That is how you get the digits of an integer:

This prints
5
3
7
5
1
9


It is reversed, but this it not a big problem. Especially when all you want is sum. Addition is commutative ;).

Secord part would be to store those digits somewhere. I recommend using a list. It can be easily reverted if you really wanted to.
So you could write a method similiar to printDigits (les's say it's named getDIgits) that returns a list of digits (integers).
Then you could write another method that sums all entries in a list.
If you need more help, post here.
 
Erica Splitt
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much! Thats my biggest problem with learning java is I seem make it more complicated than it needs to be.
Simplified the code, thanks for that tip.
Still not sure how to do the list so the individual integers can be saved and referenced again for the addition portion though. Do I add another "while" instance at the end?

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You appear not to have noticed what PP said about getting your code out of the main method.
You want a method which reads from the keyboard. You want a method which splits the number, and something to add the digits. The object‑oriented way to do that would be to have the number as the field of an object. Also some way to record the result of splitting, maybe.

Your assertion will not work; that is not what assertions are designed for. They are intended to testing, and are inactivated by default. You have to use the -ea flag (or similar) when running your code to enable assertions, because assertions are not intended to be used in production code. You will have to work out some different way to deal with negative numbers.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That assert statement is my fault. I put it in the code in my post.
I did it to clarify that my method will not work when the number passed is negative. Maybe I should have clarified this in the post...
Actually... my method won't work for 0, too . So the assertion is wrong.
Unfortunately Erica copied my method and merged with her main method.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to fix a confusion about my assert statement and to provide a little help I'm going to post a code for method that prints digits of any int number.
Notice that there is a special case for Integer.MIN_VALUE. Why? Find out what is the value of -Integer.MIN_VALUE ;).



A task for Erica.
Based on printDigits method, create a method with signature public static List<Integer> getDigits(int number) that returns a list of digits.
Do not put this into main!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!