IMEI Validator Using Java Swing

Introduction

 
This document covers how to create a simple IMEI validator application using Java Swing.
 

What is IMEI Number

 
IMEI stands for International Mobile Equipment Identity. IMEI is used to identify a mobile device when it is connected to a network. Each GSM, CDMA or satellite mobiles have unique IMEI number. This number will be printed in the device inside the battery component. User can find his device IMEI number by calling "*#06#". IMEI is a 15 digit number and the last digit is called as "Check Digit" and it can be identified by using Luhn Algorithm.
 

Luhn Algorithm

 
Luhn Algorithm is also known as "Modulus 10" algorithm. It is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, National Provider Identifier numbers in US and Canadian Social Insurance Numbers. It was created by IBM scientist Hans Peter Luhn. Verification is done by validating check digit.
  1. Double the value of every second digit from the right end(first right will be check digit number).
  2. Add the individual digits comprising both the products from step (1) and unaffected digits in the original number.
  3. If the total modulo 10 is equal to 0, then the number is valid, else it is not valid.
A simple example: IMEI no of mobile-354557030810924
 
Step 1
 
IMEI Validator Using Java Swing 
 
Step 2
 
3+1+0+4+1+0+5+1+4+0+6+0+1+6+1+0+9+4+4=50
 
Step 3
 
50%10=0. So above number is valid number.
 

IMEI Validator Using Java Swing

  1. Open Eclipse and create new Java project.

    IMEI Validator Using Java Swing

    IMEI Validator Using Java Swing
     
  2. Name the project as ImeiValidator and click Finish.

    IMEI Validator Using Java Swing
     
  3. Now open the Package Explorer and right-click on ImeiValidator.

    IMEI Validator Using Java Swing
     
  4. Create a new class called Imeivalidator:

    IMEI Validator Using Java Swing

    IMEI Validator Using Java Swing
     
  5. Write the following code in the class:
    1. import javax.swing.*;  
    2. import java.awt.BorderLayout;  
    3. import java.awt.event.*;  
    4. public class Imei {  
    5.     JFrame frame;  
    6.     JButton button;  
    7.     JTextField field;  
    8.     JLabel label;  
    9.     JLabel warninglabel;  
    10.     Box panel;  
    11.     public static void main(String[] args) {  
    12.         Imei hl = new Imei();  
    13.         hl.gui();  
    14.     }  
    15.     public void gui() {  
    16.         panel = Box.createVerticalBox();  
    17.         frame = new JFrame();  
    18.         button = new JButton("Click");  
    19.         field = new JTextField(15);  
    20.         field.putClientProperty("JComponent.sizeVariant""mini");  
    21.         label = new JLabel("Enter the IMEI Number");  
    22.         warninglabel = new JLabel("");  
    23.         //adding contents to frame  
    24.         panel.add(label);  
    25.         panel.add(field);  
    26.         panel.add(warninglabel);  
    27.         panel.add(button);  
    28.         frame.getContentPane().add(BorderLayout.NORTH, panel);  
    29.         frame.setVisible(true);  
    30.         frame.setSize(300, 300);  
    31.         button.addActionListener(new buttonAction());  
    32.     }  
    33.     public class buttonAction implements ActionListener {  
    34.         public void actionPerformed(ActionEvent ev) {  
    35.             int sum = 0;  
    36.             String ImeiNo = field.getText();  
    37.             if (ImeiNo.length() != 15) {  
    38.                 warninglabel.setText("IMEI Number should contain 15 characters");  
    39.             } else  
    40.             {  
    41.                 boolean errorflag = false;  
    42.                 for (int i = 0; i <= 14; i++) {  
    43.                     //getting ascii value for each character  
    44.                     char c = ImeiNo.charAt(i);  
    45.                     int number = c;  
    46.                     //Assigning number values to corrsponding Ascii value  
    47.                     if (number < 48 || number > 57) {  
    48.                         warninglabel.setText("Enter only numerals");  
    49.                         errorflag = true;  
    50.                         break;  
    51.                     } else  
    52.                     {  
    53.                         switch (number) {  
    54.                             case 48:  
    55.                                 number = 0;  
    56.                                 break;  
    57.                             case 49:  
    58.                                 number = 1;  
    59.                                 break;  
    60.                             case 50:  
    61.                                 number = 2;  
    62.                                 break;  
    63.                             case 51:  
    64.                                 number = 3;  
    65.                                 break;  
    66.                             case 52:  
    67.                                 number = 4;  
    68.                                 break;  
    69.                             case 53:  
    70.                                 number = 5;  
    71.                                 break;  
    72.                             case 54:  
    73.                                 number = 6;  
    74.                                 break;  
    75.                             case 55:  
    76.                                 number = 7;  
    77.                                 break;  
    78.                             case 56:  
    79.                                 number = 8;  
    80.                                 break;  
    81.                             case 57:  
    82.                                 number = 9;  
    83.                                 break;  
    84.                         }  
    85.                         //Double the even number and divide it by 10. add quotient and remainder  
    86.                         if ((i + 1) % 2 == 0) {  
    87.                             number = number * 2;  
    88.                             number = number / 10 + number % 10;  
    89.                         }  
    90.                         sum = sum + number;  
    91.                     }  
    92.                 }  
    93.                 // Check the error flag to avoid overWriting of Warning Lable  
    94.                 if (!errorflag) {  
    95.                     if (sum % 10 == 0) {  
    96.                         warninglabel.setText("Valid");  
    97.                     } else  
    98.                     {  
    99.                         warninglabel.setText("Invalid");  
    100.                     }  
    101.                 }  
    102.             }  
    103.         }  
    104.     }  
    105. }                            
    IMEI Validator Using Java Swing
     
  6. Now run the application using Run -> Run As -> Java Application.

    IMEI Validator Using Java Swing
     
  7. The following window will be displayed:

    IMEI Validator Using Java Swing
     
  8. Enter the IMEI number and click "Click" as shown below:

    IMEI Validator Using Java Swing
References
  1. http://en.wikipedia.org/wiki/Luhn_algorithm
  2. http://en.wikipedia.org/wiki/International_Mobile_Equipment_Identity