# Adding a check digit to a ID number

Page 1 of 1

## 5 Replies - 377 Views - Last Post: 09 October 2012 - 10:09 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294779&amp;s=bacec263fa5bf099a15affc005283960&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Exceedinglife

Reputation: 0
• Posts: 23
• Joined: 01-July 12

# Adding a check digit to a ID number

Posted 08 October 2012 - 01:02 PM

I have a program that i have been working on and i am pretty stuck here. I want to add a check digit that is divisble by 7 to any number inputted in the program.

Modify the main method that tests your sumDigits method to do the following: input an identification number (a positive integer), then determine if the sum of the digits in the identification number is divisible by 7 (use your sumDigits method but don't change it -- the only changes should be in main). If the sum is not divisible by 7 print a message indicating the id number is in error; otherwise print an ok message. (FYI: If the sum is divisible by 7)

I know to check this soultion i would use:
(sum % 7 == 0)

```import java.util.Scanner;

// *******************************************************************
//   DigitPlay.java
//
//   Finds the number of digits in a positive integer.
// *******************************************************************

public class DigitPlay
{

public static void main (String[] args)
{
Scanner conIn = new Scanner(System.in);

int num;    //a number

System.out.println ();
System.out.print ("Please enter a positive integer: ");

if (conIn.hasNextInt())
num = conIn.nextInt();
else
{
System.out.println("Error: you must enter an integer.");
System.out.println("Terminating program.");
return;
}
System.out.println();

if (num <= 0)
System.out.println ( num + " isn't positive -- start over!!");
else
{
// Call numDigits to find the number of digits in the number
// Print the number returned from numDigits
System.out.println ("\nThe number " + num + " contains " +
+ numDigits(num) + " digits.");
System.out.println ();
System.out.println ("\nThe sum " + num + " contains " +
+ sumDigits(num));
}
}

// -----------------------------------------------------------
//  Recursively counts the digits in a positive integer
// -----------------------------------------------------------
public static int numDigits(int num)
{
if (num < 10)
return (1);
else
return (1 + numDigits(num/10));
}
public static int sumDigits(int num)
{
if (num ==1)
return (1);
else
return (sumDigits(num-1)+num);
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Adding a check digit to a ID number

### #2 Ryano121

• D.I.C Lover

Reputation: 1055
• Posts: 2,234
• Joined: 30-January 11

## Re: Adding a check digit to a ID number

Posted 08 October 2012 - 01:19 PM

If you already know how to do it, whats the problem?

### #3 rfs02

Reputation: 26
• Posts: 70
• Joined: 30-September 12

## Re: Adding a check digit to a ID number

Posted 08 October 2012 - 01:39 PM

The way your sumDigits method is written, you are adding the numbers from 1 to the integer that was entered, i.e., if the input is 15, sumDigits will calculate the sum of 15 + 14 + 13 + etc... for a total of 120.

Other than that I agree with Ryano121, what's the question????

### #4 Exceedinglife

Reputation: 0
• Posts: 23
• Joined: 01-July 12

## Re: Adding a check digit to a ID number

Posted 09 October 2012 - 08:19 AM

I was giving these instructions and i would like someone to clarify on what im suppose to do. I dont know how to make the method:
if i'm entering a number 45225

then i need it to add:

4+5+2+2+5 = 16

i need a method that can do this to any number then i have to check if it is divisible by 7

### #5 Ryano121

• D.I.C Lover

Reputation: 1055
• Posts: 2,234
• Joined: 30-January 11

## Re: Adding a check digit to a ID number

Posted 09 October 2012 - 08:41 AM

Check out the String.charAt() method along with Integer.parseInt()

### #6 rfs02

Reputation: 26
• Posts: 70
• Joined: 30-September 12

## Re: Adding a check digit to a ID number

Posted 09 October 2012 - 10:09 AM

Actually, your code is very close - your countDigits is working fine as I am sure you realize.

What you need to do is update your sumDigits method.

If you want to use recursion (I am assuming this is what your assignment is about), then here is a way to do it:

• If num < 10, then the sum of the digits in num is actually num
• Otherwise, the sum of the digits is (num % 10) + sum of the digits in num / 10

From your code, I am assuming this only needs to work for positive integers, so no checks for decimal points or -ve signs are required.