Home > Software design >  How to verify OTP in Java using simple loop?
How to verify OTP in Java using simple loop?

Time:10-07

I'm a Java beginner and my project consists of creating a simple program to register users for an alumni center. The process creates an ID and then provides the new user with an OTP. Next is the login (Enter ID:, Enter OTP: ).

My OTP verification method is not working. It seems to be a problem with the IF.equals declaration, the process jumps straight to the ELSE condition.

Any suggestions why?

Here is my code:

class Main {
    static NewRegandLogin newRegAndLogin = new NewRegandLogin(null, null, null, null, null, null);
    static ArrayList<NewRegandLogin> loginInformation = new ArrayList<>();
    public static void main(String[] args) {

        System.out.println("        WELCOME TO THE ALUMNI SHE-CODES SYSTEM        ");
        System.out.println("_________________________________\n   - New Alumni registration - \n");
        System.out.println("");
        newRegAndLogin.registerNewGrad();
        System.out.println();
        System.out.println("_________________________________");
        System.out.println();   
        System.out.println("Your new Alumni ID is: "    newRegAndLogin.getAlumniId());
        System.out.println();
        System.out.println("Your temporary password is:");
        System.out.println(newRegAndLogin.oTp(8));
        loginInformation.add(newRegAndLogin);
        System.out.println("_________________________________");
        System.out.println("_________________________________\n   - Alumni Login - \n");
        System.out.println("");
        newRegAndLogin.login();
        System.out.println(""); 
        System.out.println("Please make a list of completed Courses: -->Enter 'S' to stop adding courses<--");
        newRegAndLogin.setAlumniCourses();
        System.out.println("_________________________________");
        newRegAndLogin.setLinkedInPage();
        loginInformation.add(newRegAndLogin);
        //printAlumniProfile();
        System.out.println("_________________________________");
        newRegAndLogin.jobOffer();
        }
void login() {
    System.out.print("ID: ");
    alumniIdImput = scanner.nextLine();
    idVerification();
    do {
        System.out.println("Password (OTP if logging in for the first time): ");
        passwordImput = scanner.nextLine();
        oTpFromImput = passwordImput.toCharArray();
        oTpVerification();
    } while (isPasswordCorrect=false);

void oTpVerification() {
    isPasswordCorrect = false;
    if (oTpFromImput.equals(oTp(8))) {
        isPasswordCorrect = true;
        System.out.println("Logging In.....");
    }else {
        isPasswordCorrect = false;
        System.out.println("Incorrect password.\nPlease enter valid password: 8 alpha numeric 
                               characters(Aa,123,@,#,$,%)");
    }   
}

This is the oTp method

char[] oTp (int length) {       
        String capitalChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String smallChars = "abcdefghijklmnopqrstuvwxyz";
        String numbers = "0123456789";
        String symbols = "!@#$%^&*_-= /.?<>";
                
        String values = capitalChars   smallChars   numbers   symbols;
                
        Random oneTimePassword = new Random();
        char[] password = new char[length];
                
        for(int i = 0; i<length;i  ) {
            password[i] = values.charAt(oneTimePassword.nextInt(values.length()));
        }
        return password;
    }

CodePudding user response:

It seems you built a guessing game, not an OTP verification code.

You first read the OTP from user, and only then randomly generate one to copare to it.

Basically, you code expects the user to guess a random 8 character password that has not been created you, which is basically impossible...

You need to generate to OTP first, show it to the user, then ask them to input it.

CodePudding user response:

I see your logic code is generate OTP code after User input. It seem so wierd bro.

Whenever you call oTp(8) function will generate new OTP.

Use should generate OTP first then store somewhere, then User input and compare it.

CodePudding user response:

You need to store the generated otp somewhere. Then compare it with the input otp. Right now you are comparing it with the otp(8). And otp(8) always returns a new otp.

  • Related