Results 1 to 8 of 8
Thread: adding numbers
 03092011, 03:57 AM #1Senior Member
 Join Date
 Feb 2011
 Posts
 255
 Rep Power
 6
adding numbers
i am trying to add every number (i had 141062). i broke them apart, and convert them to ints. then i can add them. am i going about this the right way? here is what i have so far:
Java Code:for (count = 0; i<doubledDigits.length(); i++) { numbersToAdd2 = doubledDigits.charAt(i); // gets 2610 as a string numbers4 = Integer.parseInt(numbersToAdd2); //string==>int numbers3 = numbers3 + numbers4; }
 03092011, 04:05 AM #2
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 11
Are you trying to calculate digit sum of a number?
Looks like it's correct though, does it compile and run?
 03092011, 04:13 AM #3Senior Member
 Join Date
 Feb 2011
 Posts
 255
 Rep Power
 6
yes, i am.
sorry, maybe this is better?...
Java Code:for (count = 0; i<doubledDigits.length(); i++) { numbers2 = doubledDigits.charAt(i); numbers3 = numbers2 + numbers3; }
 03092011, 04:17 AM #4
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 11
It should work if you loop through, parse each and add it to the sum.
You can also do it with a number directly using / and %.
 03092011, 04:21 AM #5Senior Member
 Join Date
 Feb 2011
 Posts
 255
 Rep Power
 6
here is what i have:
Java Code:for (i = 0; i < lengthCC ; i += 2) // get every other number (start 1 from the right) { nextDigit = (Character.getNumericValue(creditCardNumber.charAt(i))); doubledNextDigit = (nextDigit*2); // multiply that number at i by 2 numbers = Integer.toString (doubledNextDigit); doubledDigits = doubledDigits + numbers; // doubledDigits is in string form... } System.out.println("Your double digits are: " + doubledDigits); //WORKS!!! (261014) String string = ""; for (count = 0; i<doubledDigits.length(); i++) { numbersToAdd2 = doubledDigits.charAt(i); // gets 2610 as a string numbers4 = Integer.parseInt(numbersToAdd2); //string==>int numbers3 = numbers3 + numbers4; }
numbersToAdd2 was initialized as a int... doubledDigits is a string... so numbersToAdd2 should be an int, right?Last edited by droidus; 03092011 at 04:25 AM.
 03092011, 04:24 AM #6
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 11
In the original part try using the strong substring method instead. Then parse to integer and add it to the sum. What is the goal of the first loop?
 03092011, 04:26 AM #7Senior Member
 Join Date
 Feb 2011
 Posts
 255
 Rep Power
 6
to double each of the digits. i then set it to a string
i did this:
Java Code:String string = ""; String doubledDigits = ""; int numbers5 = 0; int numbers3 = 0; for (count = 0; i<doubledDigits.length(); i++) { string = doubledDigits.substring(i); // gets 2610 as a string numbers5 = Integer.parseInt(string); //string==>int numbers3 = numbers3 + numbers5; }
Last edited by droidus; 03092011 at 04:33 AM.
 03092011, 05:26 AM #8
Similar Threads

Adding squared numbers in a loop
By kurap3ka in forum New To JavaReplies: 5Last Post: 02102011, 09:13 AM 
adding numbers in an array together
By pds8475 in forum New To JavaReplies: 3Last Post: 01222011, 08:23 PM 
Adding numbers in an array?
By hawaiifiver in forum New To JavaReplies: 9Last Post: 01222009, 04:50 AM 
Adding numbers in a 2 dimensional array
By j0shizabeast in forum New To JavaReplies: 2Last Post: 11272007, 05:31 AM 
Adding numbers in array
By Shaolin in forum New To JavaReplies: 1Last Post: 11152007, 07:30 PM
Bookmarks