To Generate a One Time Password or Unique Identification URL | A Java Program to generate OTP ( One Time Password ) ; A Function to generate a unique OTP everytime ; All possible characters of my OTP ; String to hold my OTP ; Driver code ; Declare the length of OTP
class GFG { static String generateOTP ( int len ) { String str = " abcdefghijklmnopqrstuvwxyzABCD " + " EFGHIJKLMNOPQRSTUVWXYZ0123456789" ; int n = str . length ( ) ; String OTP = " " ; for ( int i = 1 ; i <= len ; i ++ ) OTP += ( str . charAt ( ( int ) ( ( Math . random ( ) * 10 ) % n ) ) ) ; return ( OTP ) ; } public static void main ( String [ ] args ) { int len = 6 ; System . out . printf ( " Your ▁ OTP ▁ is ▁ - ▁ % s " , generateOTP ( len ) ) ; } }