Results 1 to 3 of 3
  1. #1
    TheKnight is offline Member
    Join Date
    Jan 2009
    Posts
    1
    Rep Power
    0

    Default Calculating Individual Numbers

    Hello everyone,
    I'm new to the forums (as you can probably tell), and relatively new to java. I've fiddling with Java for awhile and have gotten a bit in (to 2D arrays), but there is one thing I still cannot figure out how to do.

    I have no clue to explain it other than, I'm trying to get output a sum of two numbers. And when i say two numbers, i mean, if you enter say, "16" it will display "7" because 6+1.

    A friend of mine says I have to convert the string to an Int (which i did), but he also says I have to declare each number as a variable. That is where I'm stuck at as I'm not too familiar with separating each number and converting them into their own variable.

    If anyone can help me, it would be most appreciated. I know it's relatively easy as I had to do something similar a long time ago, but I just can't remember what exactly to do.

    Thank you very much.

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    is that single digits only? checkout java doc for String.charAt(int).
    Then use Integer.parseInt(String) on it.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default me thinks that...

    Here's what I would do:
    • read the user input as a string
    • use the charAt() method (already suggested) read each char and turn into an int with the parseInt() method (also already suggested). Have to wrap a for loop around this.
    • each time a char is turned into in int, add it to the previous number

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Combining Individual Sounds - Urgent
    By JDCAce in forum Advanced Java
    Replies: 4
    Last Post: 12-05-2008, 06:17 AM
  2. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-15-2008, 12:46 AM
  3. Replies: 0
    Last Post: 04-04-2008, 03:46 PM
  4. Calculating the DB connection time
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-20-2008, 09:55 AM
  5. Calculating sin of a double value
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 09:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •