• 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

numerology program

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to do a numerology program for my Java class. The program takes as input the user's birthdate and name. The date has to be in mm/dd/yy format. Then we are supposed to break down the number, adding the digits of the date together, until it is a single digit, i.e., between 0 and 9.
My question is: what is the best way to do the conversion from String to individual ints? Or should I break the String down into constituent Strings? Someone suggested I do that, and then copy them into an array. I am not sure of the best way to do this. Basically, I just want to take a date, like 11/03/78 and add the individual numbers together, reducing them to one number, like this:
1 + 1 + 0 + 3 + 7 + 8 = 20 = 2 + 0 = 2
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had a similar problem except with time instead of date. I could find no shortcut, so I did it manually. The only thing I can think of to do is to parse the string up into individual characters. Then use the Wrapper classes to convert the string representations of the numbers into actual ints. Then you can add them normally.

------------------
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
My Java Games, I'm quite proud
 
Jade Davidson
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it better to use the wrapper classes than to break up the string and then cast to an int? I admit I am confused about how they are different. I know the wrapper classes are for converting primitive types to objects.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Michael's way is a good way. Jade, you cannot cast a String object to a primitive int. Try it. The wrapper classes have methods for converting in both directions: object <--> primitive.

There is more than one way to break up the String.

[This message has been edited by Marilyn deQueiroz (edited November 04, 2001).]
 
Jade Davidson
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I was confused. Thank you.
reply
    Bookmark Topic Watch Topic
  • New Topic