One Time Password Generator
CODE :
import java.util.*;
class Otpfu{
public static int generator(String num){
int l = num.length();
String otp = "";
for (int i = 1; i < l; i += 2) {
otp += String.valueOf((int) Math.pow(Character.getNumericValue(num.charAt(i)), 2));
}
String x=otp.substring(0,4);
System.out.println(x);
int m=Integer.parseInt(x);
return m; }
public static int incorrect(String num, int n, int m){
Scanner sc = new Scanner(System.in);
System.out.println("press 1 for reenter (or) 2 for resend");
int str = sc.nextInt();
if(str==2)
{
int a1 = (m*2);
String x= Integer.toString(a1);
String x1=x.substring(0,4);
int a2=Integer.parseInt(x1);
System.out.println("OTP : "+a2);
System.out.println("Please enter your Otp : ");
int y=sc.nextInt();
check(num,y,a2);
}
else if(str==1)
{
System.out.println("Please re-enter your Otp : ");
int x=sc.nextInt();
check(num,x,m);
}
return 0;
}
public static int check(String num, int n, int m){
if(n==m)
{
System.out.println("Correct");
}
else
{
System.out.println("InCorrect");
incorrect(num,n,m);
}
return 0;
}
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter your Number : ");
String num = scanner.next();
System.out.print("OTP :");
int m = generator(num);
System.out.println("Please enter your Otp : ");
int n=scanner.nextInt();
check(num,n,m);
}}