Generating OTP in Java
In this example, we will show you simple program about, How to Generate OTP in Java. The example program has been tested and shared in the post.
Example Program
package com.dineshkrish; /** * * @author Dinesh Krishnan * */ public class OTP { private static int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; public static String getOTP(int size) { final StringBuffer otp = new StringBuffer(); if (size != 0) { for (int i = 0; i < size; i++) { otp.append(numbers[(int) (Math.random() * numbers.length)]); } return otp.toString(); } return "INVAILD"; } public static void main(String[] args) { System.out.println(getOTP(4)); System.out.println(getOTP(5)); System.out.println(getOTP(6)); System.out.println(getOTP(7)); System.out.println(getOTP(8)); } }
Output
0418 19639 766684 0877528 49566906
More from my site

Hello, folks, I am a founder of idineshkrishnan.com. I love open source technologies, If you find my tutorials are useful, please consider making donations to these charities.
No responses yet