Results 1 to 9 of 9
  1. #1
    thomanis is offline Member
    Join Date
    Mar 2017
    Location
    SA
    Posts
    4
    Rep Power
    0

    Angry Luhn algorithm problem

    Good day , I have a Luhn algorithm which i'm using it to validate the ID number(textbox input) as well as to produce a valid checksum digit for new ID numbers.

    I need a help in declaring my textinput(ID number in this case) and button to validate that textinput. And how do i get these value ( return birthDate, return male, return citizen, return idNumber, return valid ) into the birthdate textview box with onclick of submit button after adding the IdNumber in the relevant textbox. res/layout/activity_main.xml is prepare already.


    Let me know if you need some more clarity.


    /**Java Implementation*/

    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    /**Generating South African ID Numbers*/

    public class IDNumberValidatorUtility {

    private IDNumberValidatorUtility() {

    }

    private static int generateLuhnDigit(String input) {
    int total = 0;
    int count = 0;
    for (int i = 0; i < input.length(); i++) {
    int multiple = (count % 2) + 1;
    count++;
    int temp = multiple * Integer.parseInt(String.valueOf(input.charAt(i)));
    temp = (int) Math.floor(temp / 10) + (temp % 10);
    total += temp;
    }

    total = (total * 9) % 10;

    return total;
    }

    public static boolean validate(String idNumber) {
    try {
    Pattern pattern = Pattern.compile("[0-9]{13}");
    Matcher matcher = pattern.matcher(idNumber);

    if (!matcher.matches()) {
    return false;
    }

    if (!validateDate(idNumber.substring(0, 6))) {
    return false;
    }
    int lastNumber = Integer.parseInt(String.valueOf(idNumber.charAt(id Number.length() - 1)));
    String numberSection = idNumber.substring(0, idNumber.length() - 1);

    return lastNumber == generateLuhnDigit(numberSection);
    } catch (Exception ex) {
    return false;
    }
    }

    private static boolean validateDate(String date) {
    int year = Integer.parseInt(date.substring(0, 2));
    int month = Integer.parseInt(date.substring(2, 4));

    if (month < 1 || month > 12) {
    return false;
    }

    int day = Integer.parseInt(date.substring(4, 6));
    Calendar myCal = new GregorianCalendar(year, month, day);
    int maxDays = myCal.getActualMaximum(Calendar.DAY_OF_MONTH);
    if (day < 1 || day > maxDays) {
    return false;
    }

    return true;
    }

    private static Date getBirthdate(String idNumber) {
    int year = Integer.parseInt(idNumber.substring(0, 2));
    int currentYear = Calendar.getInstance().get(Calendar.YEAR) % 100;

    int prefix = 1900;
    if (year < currentYear) {
    prefix = 2000;
    }
    year += prefix;

    int month = Integer.parseInt(idNumber.substring(2, 4));
    int day = Integer.parseInt(idNumber.substring(4, 6));

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(0);
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, month - 1);
    calendar.set(Calendar.DAY_OF_MONTH, day);

    return calendar.getTime();
    }

    /**The static method extractInformation that takes in the String version of the ID number and returns an IDNumberDetails object containing the information on that number*/

    public static IDNumberDetails extractInformation(String idNumber) {
    if (!validate(idNumber)) {
    return new IDNumberDetails(idNumber, false);
    }

    Date birthDate = getBirthdate(idNumber);
    boolean male = Integer.parseInt(idNumber.substring(6, 7)) >= 5;
    boolean citizen = Integer.parseInt(idNumber.substring(10, 11)) == 0;

    return new IDNumberDetails(idNumber, birthDate, male, citizen, true);
    }

    public static class IDNumberDetails {
    private String idNumber;
    private boolean valid;
    private Date birthDate;
    private boolean male;
    private boolean citizen;

    /**
    * @param idNumber
    * @param valid
    */
    public IDNumberDetails(String idNumber, boolean valid) {
    super();
    this.idNumber = idNumber;
    this.valid = valid;
    }

    public IDNumberDetails(String idNumber, Date birthDate, boolean male, boolean citizen, boolean valid) {
    this.idNumber = idNumber;
    this.birthDate = birthDate;
    this.valid = valid;
    this.male = male;
    this.citizen = citizen;
    }

    /**
    * @return the birthDate
    */
    public Date getBirthDate() {
    return birthDate;
    }

    /**
    * @return the male
    */
    public boolean isMale() {
    return male;
    }

    /**
    * @return the citizen
    */
    public boolean isCitizen() {
    return citizen;
    }

    /**
    * @return the idNumber
    */
    public String getIdNumber() {
    return idNumber;
    }

    /**
    * @return the valid
    */
    public boolean isValid() {
    return valid;
    }
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Luhn algorithm problem

    need a help in declaring my textinput(ID number in this case) and button
    Please explain what problems you are having.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    Post moved to own thread
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    thomanis is offline Member
    Join Date
    Mar 2017
    Location
    SA
    Posts
    4
    Rep Power
    0

    Default Re: Luhn algorithm problem

    I would like to find out where can i declare my textinput(ID number in this case) and button to validate that textinput from this code. And how do i get these value ( return birthDate, return male, return citizen, return idNumber, return valid ) into the birthdate textview box with onclick of submit button after adding the IdNumber in the relevant textbox

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Luhn algorithm problem

    Can't you designate a class to hold the require values and then return an instance of that class to whatever needs it?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    thomanis is offline Member
    Join Date
    Mar 2017
    Location
    SA
    Posts
    4
    Rep Power
    0

    Default Re: Luhn algorithm problem

    From the extends class i define these to hold values, but not sure where they fit in the algorithm

    public class MainActivity extends AppCompatActivity {

    Button button1;
    EditText IDno;

    TextView txtid, txtbd, txtvld, txtmale, txtct;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button1 = (Button) findViewById(R.id.Validateid);
    IDno = (EditText) findViewById(R.id.IDNumber);
    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Luhn algorithm problem

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.


    Thread moved to Android section.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Luhn algorithm problem

    A couple of observations:

    1. Your Luhn algorithm is wrong. It needs to scan the card number from right to left. If you want to go from left to right you need to prepend a 0 to account numbers with an odd number of digits.

    2. You use the Math.floor() method in one of your calculations. No need to use that for integers since integer division drops the fraction.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    thomanis is offline Member
    Join Date
    Mar 2017
    Location
    SA
    Posts
    4
    Rep Power
    0

    Default Re: Luhn algorithm problem

    Thanks Jim, I will import the scanner accordingly, where should i use math.floor() from my code? and how do i use my textinput(ID number in this case) and button to validate that textinput from this code.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Luhn algorithm problem

    Uh, I think you (one of us) misunderstood. You don't need to import the scanner. Your for loop goes from left to right starting with i = 0. Normally
    you go from right to left in this algorithm. But if you just prepend a 0 to the account number (for odd number of digits), then you can leave it as is.

    And you don't need the Math.floor() function at all. Math.floor() is only used to return the integer portion of type double.

    Java Code:
    int a = 64
    a = a/10;  //a is now 6 because a is an integer.  In your code, temp is an integer.
    For more info on the Luhn algorithm (and how to validate the account number), check Luhn Algorithm

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Luhn's Algorithm "Unexpected Type" Compiling issue.
    By William Hagen in forum New To Java
    Replies: 1
    Last Post: 10-21-2015, 02:58 AM
  2. Algorithm problem.
    By luisx86 in forum New To Java
    Replies: 2
    Last Post: 03-25-2013, 01:43 AM
  3. An algorithm problem
    By bMorgan in forum New To Java
    Replies: 1
    Last Post: 01-27-2011, 09:53 AM
  4. Problem with algorithm
    By Albert in forum AWT / Swing
    Replies: 1
    Last Post: 07-13-2007, 03:31 PM
  5. Algorithm problem
    By Marcus in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 01:37 AM

Posting Permissions

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