Java Program to Generate and Verify OTP

In this article we will see how we can generate OTP(One Time Password) by using Java programming language.

Java Program to Generate OTP & Verify OTP


One time Password in short known as OTP. It is a random set of numbers or characters which is used for security reason. To check the authentication instantly and correctly of the user, OTP is used where one OTP is shared with the user through any medium like message or mail and then it is validated.


In Java we have java.uti.Random class which is used to generate pseudorandom numbers. We have inbuilt random() method which can be used to generate random numbers.

Let’s see the program to understand it more clearly.

Program to Generate and Verify OTP:


  • Call a user defined method say createOTP() method by passing length as parameter, which refers to length of OTP. And store the result of this method which is an OTP in a string variable say resultOTP.
  • Inside method by using a for loop create the OTP by using Random().nextInt(9) method, which will generate random digit within 0-9.
  • After the for loop completion you will get an OTP is which is stored in currentOTP string variable. Return currentOTP
  • Then ask the user to enter the system generated OTP and assign it to an String variable say inputOTP.
  • Then by using inbuilt equals() method compare both the OTP i.e. system generated OTP and user input OTP.
  • If both OTP matches then OTP verification successful else Invalid OTP.


import java.util.*;

public class Main 
    static String currentOTP = "";
    static String inputOTP = "";
    static int randomNo;
    //driver method
   public static void main(String[] args) 
      //Object of Scanner class created
      Scanner sc=new Scanner(;
      //calling the user defined method createOTP() 
      //and passing the OTP length as parameter
      //and assigning the result OTP to a character variable 
      String resultOTP=createOTP(4);
      System.out.println("Your OTP: "+resultOTP);
      //Ask the user to enter the OTP
      System.out.print("Enter received OTP: ");
      inputOTP = sc.nextLine();
      if (resultOTP.equals(inputOTP))
        System.out.println("OTP verified successfully");
        System.out.println("Invalid OTP!");

   private static String createOTP(int length) 
        // Use for loop to iterate 4 times and generate random OTP
        for (int i = 0; i < length; i++) 
            //Generating random digit within 0-9, which is of type int
            randomNo = new Random().nextInt(9);
            //converting generated randomNo to type String by using toString() method
            //then concating it to the currentOTP 
            currentOTP = currentOTP.concat(Integer.toString(randomNo));
        //Return the generated OTP
        return currentOTP;


CASE-1: Invalid OTP

Your OTP: 0855
Enter received OTP: 0856
Invalid OTP!

CASE-2: Valid OTP

Your OTP: 7271
Enter received OTP: 7271
OTP verified successfully

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.