0

I am struggling with this little code in java, I want to fill in an array in console with username and password using the Scanner library, but I want to validate that the password is >=8, for some reason the array returns empty, any idea why? It only happens if I type a 8 digits character, if I do a shorter password and then an 8 digits it works. so the problem I am having is with the "if(leng>=8)" I have spent a couple hours on this but no progress so far.

import java.util.Scanner;

public class JavaApplication2 {
  public static void main(String[] arg) {
    String[] names = new String[2];
    String[] password = new String[2];
    for (int i = 0; i < names.length; i++) {
      System.out.println("Enter the Name: ");
      Scanner input = new Scanner(System.in);
      names[i] = input.nextLine();
      System.out.println("Enter the password: ");
      Scanner input2 = new Scanner(System.in);
      String validation = input2.next(); //convert the input2 variable to a string
      int leng = validation.length(); //length of the string
      System.out.println(leng); //test print
      if (leng >= 8) {
        password[i] = input2.nextLine();
        System.out.println(password[i]);
        //test printing
      } else if (leng <= 7) {
        while (leng <= 7) //Condtion to check password
        {
          System.out.println("Enter a longer password: "); //ask for a longer password
          input2 = new Scanner(System.in); //collect the input
          password[i] = input2.nextLine(); //store the password on the array
          leng = password[i].length(); //convert leng to check continuing the while loop or not
        }
      }
    }
    for (int j = 0; j < names.length; j++) {
      System.out.println(names[j] + " " + password[j]);
    }
    int k;
    Boolean flag = true;
    String temp1;
    String temp2;
    while (flag) {
      flag = false;
      for (int l = 0; l < names.length - 1; l++) {
        if (names[l].compareToIgnoreCase(names[l + 1]) > 0) {
          temp1 = names[l];
          names[l] = names[l + 1];
          names[l + 1] = temp1;
          temp2 = password[l];
          password[l] = password[l + 1];
          password[l + 1] = temp2;
          flag = true;
        }
      }
    }
  }
}
Erick Kirei
  • 37
  • 1
  • 8

0 Answers0