7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

マイナンバーチェックデジット用Javaコード

Last updated at Posted at 2016-01-11

CodeIDの回答でマイナンバーDigit番号をチェックするコード
算出方法:https://codeiq.jp/sites/default/files/answer_ready/2007/mynumber.html

public class MyNmaber {

/**
 * 11,12桁半角数字
 */
private static final String CHECK_REGEX = "[0-9]{11,12}";


public static void main( String[] args ) throws IOException {

    int c;
    StringBuilder input = new StringBuilder();
    while ( ( c = System.in.read() ) != -1 ) {

        if ( c == 10 ) {

            checkMyNmber( input.toString() );
            input = new StringBuilder();
            continue;
        }

        input.append( (char) c );

    }

}


private static void checkMyNmber( String myNmber ) {

    if ( !Pattern.matches( CHECK_REGEX, myNmber ) ) {
        System.out.println( "Error" );
        return;
    }

    String checkDigit = calcDigit( myNmber.substring( 0, 11 ) );

    if ( myNmber.length() == 11 ) {
        System.out.println( checkDigit );
    } else {
        if ( checkDigit.equals( myNmber.substring( 11, 12 ) ) ) {
            System.out.println( "OK" );
        } else {
            System.out.println( "NG" );
        }

    }
}


private static String calcDigit( String number ) {

    int sum = 0;
    for ( int i = 1; i <= 11; i++ ) {

        int pn = Integer.valueOf( number.substring( 11 - i, 12 - i ) );
        int qn = i <= 6 ? i + 1 : i - 5;
        sum += pn * qn;
    }

    int mod = sum % 11;

    if ( mod <= 1 ) {
        return "0";
    } else {
        return String.valueOf( 11 - mod );

    }

}

}

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?