Simple Login Validation Using Arrays in Java

0
39
import java.util.Scanner;

import java.util.Random;

public class LoginVal{

   public static void main(String[] args){
   
      String[] loginID = {"JCamel", "MJane","RainyDay"};
   
      String[] passwords = {"12345","qwerty","password"};
   
      Scanner input = new Scanner(System.in);
      String match = "N";
      String userID = "";
      String userPw = "";
   
      int counter=-1;
   
      System.out.println("Enter your Login id: ");
      userID = input.next();
   
      while(counter == -1){ // this loop keeps going untill a matching name is entered
        
         for (int i = 0; i < loginID.length; i++){ //this loop checks for the values in the array
             
            if (loginID[i].equals(userID)){
               counter = i;
               break;
            }
         }
        
         if (counter == -1){ //This means there was no match since counter is still -1
            System.out.println ("Invalid Login id please try again!");
            System.out.println ("Enter your Login id: ");
            userID = input.next();
         }
      }
     
     
      while(match.equals("N")){ // this loops going until a password match
         System.out.println ("Enter your password: ");
         userPw = input.next();
        
         if (passwords[counter].equals(userPw)){
            match = "Y";
            System.out.println ("You are successfull loged in  \n");
         }
         else{
            System.out.println ("Invalid password please try again!");
         }
      }
   }
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here