Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package imei;
- public class Imei {
- public static void main(String[] args) {
- boolean a = isValidIMEI("123123-12-432341-3"); // should be correct
- System.out.println(a);
- a = isValidIMEI("123122-34-342331-2"); // should be incorrect
- System.out.println(a);
- }
- public static boolean isValidIMEI(String imei){
- imei = imei.replaceAll("-","");
- if(imei.length() < 15)
- return false;
- int sum_even = 0;
- for(int i=0; i<7; i++){
- char temp = imei.charAt(i*2);
- int number = Integer.parseInt(String.valueOf(temp));
- sum_even += number;
- }
- int sum_odd = 0;
- int x = 0;
- for(int i=0; i<7; i++){
- char temp = imei.charAt(i*2+1);
- int number = Integer.parseInt(String.valueOf(temp));
- x = 2*number;
- if(x<10)
- sum_odd += x;
- else
- sum_odd += Math.floor(x/10) + x%10;
- }
- int sum = sum_even + sum_odd;
- int check_sum = Integer.parseInt(String.valueOf(imei.charAt(14)));
- int result = 0;
- if(sum%10 > 0){
- result = 10 - sum%10;
- }
- else{
- result = 0;
- }
- if(result == check_sum)
- return true;
- else return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement