• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to calculate numbers until it returns a single number?

 
Ranch Hand
Posts: 507
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am in a situation like this. I have a 3 JSpinners/JTextFields to accept user's birthday. one JSpinner to take the year, next to take the month and next to take the date. Once the user clicks the "OK" button, those year,month,date should sum together untill it is a SINGLE number value, like below

Year: 1990
Month: 6
Date: 3

when the button is clicked,

1990+6+3
1999

1+9+9+9
28

2+8
10

1+0
1

answer=1


How can I do this? Please help me..
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where are you stuck?
- Starting this code from the OK button? Use an ActionListener or Action for the JButton.
- The algorithm itself? Well, you've already written down the steps to take. All you need to do is translate that into code:
-- one single number means >= 0 and < 10.
-- using "% 10" you can get the last digit.
-- using "/ 10" you can cut off the last digit.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, and you can use a single JSpinner with a SpinnerDateModel and a JSpinner.DateEditor that has an appropriate date format pattern.
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:Where are you stuck?
- Starting this code from the OK button? Use an ActionListener or Action for the JButton.
- The algorithm itself? Well, you've already written down the steps to take. All you need to do is translate that into code:
-- one single number means >= 0 and < 10.
-- using "% 10" you can get the last digit.
-- using "/ 10" you can cut off the last digit.



OK. But how can I separate 1990 into 4 digits? (1+9+9+0), and calculate it until I get a single place number?

Following is an sample I tried. But it didn't work



please help
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:Oh, and you can use a single JSpinner with a SpinnerDateModel and a JSpinner.DateEditor that has an appropriate date format pattern.



Thanks for the suggestion. When I am doing the actual design, I will look into that
 
Bartender
Posts: 563
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The sum of the year, month, and day will be a number in the thousands, a 4-digit number.

You then break the 4-digit number apart into each of the 4 digits. if the number is 1987, what is:

1987 % 10 = ?
198 % 10 = ?
19 % 10 = ?

I leave to you how to get 198, 19, and then 1.

Then add the 4 results together, in this case 7 + 8 + 9 + 1 = 25.

If the result is > 10, repeat. Any time you're repeating something, consider generalizing the algorithm and writing it in a method. In this case the method might be


Good luck!
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Greg Brannon wrote:The sum of the year, month, and day will be a number in the thousands, a 4-digit number.

You then break the 4-digit number apart into each of the 4 digits. if the number is 1987, what is:

1987 % 10 = ?
198 % 10 = ?
19 % 10 = ?

I leave to you how to get 198, 19, and then 1.

Then add the 4 results together, in this case 7 + 8 + 9 + 1 = 25.

If the result is > 10, repeat. Any time you're repeating something, consider generalizing the algorithm and writing it in a method. In this case the method might be


Good luck!



Thanks for the reply. It helped a lot


Thanks all of you for the help. I really appreciate that All of your comments opened me a gateway for another way of thinking. Following is my code



I know it is messy. This is just a testing sample, I will clear it up when I am doing the actual design. I will start that tomorrow. Thanks for all of you again...
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It'll be interesting to see the cleaned up version so please share it when you're done.

Does your current approach work if age = 2001? Or, for your original example, if age = 1999? You may need to add some additional testing to determine if you should stop or continue with the algorithm.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey, try this method:



hope it is what you wanted
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

There are much more elegant solutions available than that, I am afraid.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And even if you do want to go the int -> String -> int way, there are some improvements to be made:

jandries Aldum wrote:


- Don't concatenate a number to an empty String to convert it to a String. Use Integer.toString or String.valueOf. These static methods are designed to do just that.
- Adding these three numbers like this is going to cause you problems. What will be the difference between February 1st 2011 and January 2nd 2011? Both lead to 2014 as total. You probably want something like 20110102 and 20110201 instead.
- You know that luck only has digits. You can then use luck.charAt(i) - '0' to get the int value of such a digit, or even better use Character.digit(luck.charAt(i), 10).

But yeah, using only int arithmetic you can do this without using Strings etc.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... or there is a numericValue method in the Character class which converts some chars to small numbers.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote: . . . What will be the difference between February 1st 2011 and January 2nd 2011? Both lead to 2014 as total. . . .

I thought the idea was to reduce the values to a single digit. In which case both those dates will come out to 7 and today (20110718 or 18/7/2011) will come out as 2.

The transverse sum of the the digits of a decimal natural number gives the remainder when the number is divided by (10 - 1 =) 9. You can do the same in hex and get the remainder when divided by f
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Rob Spoor wrote: . . . What will be the difference between February 1st 2011 and January 2nd 2011? Both lead to 2014 as total. . . .

I thought the idea was to reduce the values to a single digit. In which case both those dates will come out to 7 and today (20110718 or 18/7/2011) will come out as 2.


You're right of course. It doesn't matter if we add 1+1+2 (the last digit of each number) right now or one step later, they are added anyway.
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Greg Brannon wrote:It'll be interesting to see the cleaned up version so please share it when you're done.

Does your current approach work if age = 2001? Or, for your original example, if age = 1999? You may need to add some additional testing to determine if you should stop or continue with the algorithm.



Very good question. It is fine with 2001, not with 1999. For the final result calculation, which occurs after separating these into 4 parts,I am gonna add an if condition with a part of "jandries Aldum"'s code.
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jandries Aldum wrote:hey, try this method:



hope it is what you wanted



Good thought. After concerning the comments given by the experienced people, we can make this bug free
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All right, just imagine that I have taken the following post from my privous post and I edited that with the if else as I mentioned above. So, in tha case, is that good / ok , if I put that to my actual design as below?

 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try it and see.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code is difficult to readbecauseyouhavenotleftanyspacebetweentokens.
Not assign=age%10 but assign = age % 10, please. One space before and after each binary operator.

That code appears too specific. There is a simple and elegant way to do it with loops, which can be generalised for any integer.
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Your code is difficult to readbecauseyouhavenotleftanyspacebetweentokens.
Not assign=age%10 but assign = age % 10, please. One space before and after each binary operator.

That code appears too specific. There is a simple and elegant way to do it with loops, which can be generalised for any integer.



I understand. And the link was awesome
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... and have you found the generalisation yet?
 
reply
    Bookmark Topic Watch Topic
  • New Topic