Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java contains password input

import java.util.Scanner;

public class PromptPassword {

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.print("Password rules:
"
                            + "Password must have at least eight characters
"
                            + "Password must consist of only letters and digits
"
                            + "Password must contain at least two digits
"
                            + "Enter a password:");
        String pWT = sc.nextLine(); //read the entire line
        passWordIsValid(pWT.trim()); // to remove leading spaces (if any)
    }   
    public static void passWordIsValid (String password) {
        boolean passWordIsValid = true;
        int noOfDigits =0;


        if (password.length() > 8) { // if it's less than 8 chars -> no need to continue
            for(char c : password.toCharArray()){
                if(Character.isDigit(c)){ // if it's a digit, increment
                    noOfDigits++;
                }
                else if(!Character.isAlphabetic(c)){ // if it's not a digit nor alphapet
                    passWordIsValid = false;
                    break;
                }
            }
        }

         if (passWordIsValid && noOfDigits>=2){
             System.out.print("Password is valid");
         }
         else{
             System.out.println("Password is invalid"); 
         }

    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: find number of days between two local dates in java 8 
Java :: spring create bean only if another bean does not exists 
Java :: null checker on addAll java 
Java :: how to detenct free the end achievement spigot 
Java :: java check if bundle has property 
Java :: string array to stream 
Java :: puissance java 
Java :: java get specific element from arraylist 
Java :: spigot run code asynhronously 
Java :: how to parse json array in java 
Java :: how to convert a hexadecimal number to a decimal inside a println in java 
Java :: pyramid star pattern in java 
Java :: take string until / 
Java :: How to find a target value within a binary search tree, in Java? 
Java :: java initialize map with values in one line 
Java :: Youtube video thumbnail not load in android 11 or above progmatically 
Java :: spigot action bar 
Java :: Get the first Monday of a month in Java 
Java :: immagini java 
Java :: the result o trim is ignored java 
Java :: How to perform an iterative depth first search through a binary tree, in Java? 
Java :: how to add image in title bar in android 
Java :: spring mvc get all request parameters 
Java :: assert system.out.println 
Java :: java get distinct values from list 
Java :: java selenium select 
Java :: int to binary java 
Java :: kotlin textview center text 
Java :: How to efficiently find the highest power of 2 dividing a given number, in Java? 
Java :: array to arraylist 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =