Results 1 to 8 of 8

Thread: adding numbers

  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

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

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Are you trying to calculate digit sum of a number?

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

  3. #3
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    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. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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. #5
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    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 03:25 AM.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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. #7
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    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 03:33 AM.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    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.

Similar Threads

  1. Adding squared numbers in a loop
    By kurap3ka in forum New To Java
    Replies: 5
    Last Post: 02-10-2011, 08:13 AM
  2. adding numbers in an array together
    By pds8475 in forum New To Java
    Replies: 3
    Last Post: 01-22-2011, 07:23 PM
  3. Adding numbers in an array?
    By hawaiifiver in forum New To Java
    Replies: 9
    Last Post: 01-22-2009, 03:50 AM
  4. Adding numbers in a 2 dimensional array
    By j0shizabeast in forum New To Java
    Replies: 2
    Last Post: 11-27-2007, 04:31 AM
  5. Adding numbers in array
    By Shaolin in forum New To Java
    Replies: 1
    Last Post: 11-15-2007, 06:30 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
  •