5 Replies - 1366 Views - Last Post: 09 October 2012 - 10:09 AM Rate Topic: -----

#1 Exceedinglife  Icon User is online

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • 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?
Was This Post Helpful? 1
  • +
  • -

#3 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • 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????
Was This Post Helpful? 0
  • +
  • -

#4 Exceedinglife  Icon User is online

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • 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()
Was This Post Helpful? 0
  • +
  • -

#6 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1