1. Senior Member
Join Date
Feb 2011
Posts
244
Rep Power
4

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;
}```
sorry about the variables... please ignore for now :p

2. Are you trying to calculate digit sum of a number?

Looks like it's correct though, does it compile and run?

3. Senior Member
Join Date
Feb 2011
Posts
244
Rep Power
4
yes, i am.

sorry, maybe this is better?...

Java Code:
```	for (count = 0; i<doubledDigits.length(); i++)
{
numbers2 = doubledDigits.charAt(i);
numbers3 = numbers2 + numbers3;
}```
still doesn't work tho... i get zip!

4. 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 %.

5. Senior Member
Join Date
Feb 2011
Posts
244
Rep Power
4
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;
}```
i get a cannot find symbol for the parsing, and incompatible types for numbers3 + 4... i guess the first problem creates a second problem :(

numbersToAdd2 was initialized as a int... doubledDigits is a string... so numbersToAdd2 should be an int, right?
Last edited by droidus; 03-09-2011 at 04:25 AM.

6. 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?

7. Senior Member
Join Date
Feb 2011
Posts
244
Rep Power
4
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;
}```
and still got 0.
Last edited by droidus; 03-09-2011 at 04:33 AM.

8. If this is your actual code then the doubleDigits string is empty (has a length of zero) so it never enters the loop and the value of numbers3 (please use more appropriate variable names) never changes.

#### Posting Permissions

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