Results 1 to 3 of 3
Thread: Calculating Individual Numbers
- 01-30-2009, 12:12 AM #1Member
- Join Date
- Jan 2009
- Rep Power
Calculating Individual Numbers
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.
- 01-30-2009, 12:25 AM #2
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)
- 01-30-2009, 12:51 AM #3
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
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- By JDCAce in forum Advanced JavaReplies: 4Last Post: 12-05-2008, 05:17 AM
- By trofyscarz in forum New To JavaReplies: 2Last Post: 10-14-2008, 11:46 PM
- By Java Tip in forum java.utilReplies: 0Last Post: 04-04-2008, 02:46 PM
- By Java Tip in forum Java TipReplies: 0Last Post: 01-20-2008, 08:55 AM
- By Java Tip in forum Java TipReplies: 0Last Post: 01-13-2008, 08:13 PM