Saturday 9 February 2019

JAVA : How to generate OTP


OtpExample.java
package java8s;

import java.util.*;

public class OtpExample {
    
     static char[] show(int len)
    { 
        System.out.print("Your OTP is : ");
 
        String numbers = "0123456789";
 
        Random rd = new Random();
 
        char[] otp = new char[len];
 
        for (int i = 0; i < len; i++)
        {
            
            otp[i] =
             numbers.charAt(rd.nextInt(numbers.length()));
        }
        return otp;
    }

     public static void main(String[] args) {
          
           int length = 5;
        System.out.println(show(length));

     }

}

Output:
Your OTP is : 35838

Thanks & Regards:
Trilochan Tarai
SILAN Technology & java8s.com
+91-9658317757

No comments:

Post a Comment