15,037,420 members
Articles / Desktop Programming / Swing
Article
Posted 5 Jul 2012

34.9K views
13 bookmarked

# IMEI Validator using Java Swing

Rate me:
How to create a simple IMEI validator application using Java Swing

## Introduction

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

## 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 mobile has a unique IMEI number. This number will be printed in the device inside the battery component. A 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 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 the above number is a valid number.

## Steps to Develop the Application

1. Open Eclipse and create a new Java project.

2. Name the project as `ImeiValidator` and click Finish.

3. Now open Package explorer and right click on `ImeiValidator`.

4. Create new class called `Imeivalidator`.

5. Write the following code in the class:
Java
```import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.*;

public class Imei {
JFrame frame;
JButton button;
JTextField field;
JLabel label;
JLabel warninglabel;
Box panel;

public static void main(String[] args) {
Imei hl=new Imei();
hl.gui();
}
public void gui(){

panel = Box.createVerticalBox();
frame=new JFrame();
button=new JButton("Click");
field=new JTextField(15);
field.putClientProperty("JComponent.sizeVariant", "mini");

label=new JLabel("Enter the IMEI Number");
warninglabel=new JLabel("");

frame.setVisible(true);
frame.setSize(300,300);

}
public class buttonAction implements ActionListener{
public void actionPerformed(ActionEvent ev) {
int sum=0;
String ImeiNo=field.getText();
if (ImeiNo.length()!=15){
warninglabel.setText("IMEI Number should contain 15 characters");
}else
{
boolean errorflag = false;
for(int i=0;i<=14;i++){
//getting ascii value for each character
char c=ImeiNo.charAt(i);
int number=c;
//Assigning number values to corresponding ASCII value
if (number<48 || number>57){
warninglabel.setText("Enter only numerals");
errorflag = true;
break;
}else
{
switch(number){
case 48: number=0;break;
case 49: number=1;break;
case 50: number=2;break;
case 51: number=3;break;
case 52: number =4;break;
case 53:number =5;break;
case 54:number=6;break;
case 55:number=7;break;
case 56:number=8;break;
case 57:number=9;break;
}
//Double the even number and divide it by 10.
if ((i+1)%2==0){
number=number*2;
number=number/10+number%10;
}
sum=sum+number;
}
}
// Check the error flag to avoid overWriting of Warning Lable
if(!errorflag){
if(sum%10==0){
warninglabel.setText("Valid");
}
else
{
warninglabel.setText("Invalid");
}
}
}
}
}
}```

6. Now run the application by Run->Run As->Java Application.

7. The following window will get displayed:

8. Enter the IMEI number and click ‘Click’ as shown below:

## History

• 5th July, 2012: Initial version

## Share

 Technical Lead EF (Education First) India
I graduated as Production Engineer and started my career as Software Developer then worked as tester for a while before moving into Windows application development using Microsoft Technologies. But for the last few years i am working on javascript, React, Node, AWS, Azure Chatbots

 First Prev Next
 IMEI Validator using javafx and java Stream API Aamir Khan7-Nov-15 23:14 Aamir Khan 7-Nov-15 23:14
 Code Improvements Psycho_Coder19-May-14 8:20 Psycho_Coder 19-May-14 8:20
 Your Gui code can be improved. You shouldn't just start a GUI but its a good programming practice to start the GUI in the main event thread. So you can use
SwingUtilities.invokeLater(new Runnable(){  @Override public void run(){ //Start your gui here }  });
You should encapsulate the fields and methods and not just make them public. Use a constructor and make initialize the gui controls with null. Its better not to use Box. Read the docs it says "Warning: Serialized objects of this class will not be compatible with future Swing releases. " http://docs.oracle.com/javase/7/docs/api/javax/swing/Box.html[^]
import javax.swing.*; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.*;  public class Imei {      private JFrame frame;     private JButton button;     private JTextField field;     private JLabel label;     private JPanel panel;      public Imei() {         frame = null;         button = null;         field = null;         label = null;         panel = null;     }      public static void main(String[] args) {         SwingUtilities.invokeLater(new Runnable() {              @Override             public void run() {                 Imei hl = new Imei();                 hl.gui();             }         });      }      private void gui() {          panel = new JPanel(new BorderLayout());         frame = new JFrame("IMEI Validator");          button = new JButton("Click");         field = new JTextField(15);         field.putClientProperty("JComponent.sizeVariant", "mini");          label = new JLabel("Enter the IMEI Number");          //adding contents to frame         panel.add(label, BorderLayout.NORTH);         panel.add(field, BorderLayout.CENTER);         panel.add(button, BorderLayout.SOUTH);          button.addActionListener(new ActionListener() {              @Override             public void actionPerformed(ActionEvent e) {                 int sum = 0;                 String ImeiNo = field.getText();                 if (ImeiNo.length() != 15) {                     JOptionPane.showMessageDialog(panel, "IMEI Number should contain 15 characters", "Error", JOptionPane.ERROR_MESSAGE);                  } else {                     boolean errorflag = false;                     for (int i = 0; i <= 14; i++) {                         //getting ascii value for each character                         char c = ImeiNo.charAt(i);                         int number = c;                         //Assigning number values to corrsponding Ascii value                         if (number < 48 || number > 57) {                             JOptionPane.showMessageDialog(panel, "Enter Numbers Only", "Error", JOptionPane.ERROR_MESSAGE);                             errorflag = true;                             break;                         } else {                             switch (number) {                                 case 48:                                     number = 0;                                     break;                                 case 49:                                     number = 1;                                     break;                                 case 50:                                     number = 2;                                     break;                                 case 51:                                     number = 3;                                     break;                                 case 52:                                     number = 4;                                     break;                                 case 53:                                     number = 5;                                     break;                                 case 54:                                     number = 6;                                     break;                                 case 55:                                     number = 7;                                     break;                                 case 56:                                     number = 8;                                     break;                                 case 57:                                     number = 9;                                     break;                             }                             //Double the even number and divide it by 10. add quotient and remainder                             if ((i + 1) % 2 == 0) {                                 number *= 2;                                 number = number / 10 + number % 10;                             }                             sum += number;                         }                     }                     // Check the error flag to avoid overWriting of Warning Lable                     if (!errorflag) {                         if (sum % 10 == 0) {                             JOptionPane.showMessageDialog(panel, "Valid Imei", "Valid Imei", JOptionPane.PLAIN_MESSAGE);                         } else {                             JOptionPane.showMessageDialog(panel, "Invalid Imei", "Invalid Imei", JOptionPane.INFORMATION_MESSAGE);                         }                     }                 }             }         });          frame.setContentPane(panel);         frame.setSize(new Dimension(400, 100));         frame.setLocationRelativeTo(null);         frame.setVisible(true);         frame.setResizable(false);         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);     } }
I would prefer to do like this and its better this way. A few more optimizations can be done but I will leave it for now. "Don't do anything by half. If you love someone, love them with all your soul. When you go to work, work your ass off. When you hate someone, hate them until it hurts."--- Henry Rollins
 Nice one umangm9-Jul-12 19:59 umangm 9-Jul-12 19:59
 Re: Nice one Santhosh Kumar Jayaraman5-Sep-12 21:25 Santhosh Kumar Jayaraman 5-Sep-12 21:25
 Last Visit: 31-Dec-99 18:00     Last Update: 24-Sep-21 0:12 Refresh 1