Click here to Skip to main content
15,886,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I write a Java Program That prompts the user to enter a password and displays it back in the console window. The displayed password should be the entered password halved with the full password total number of characters added at the end.

example1:
enter your password: abcd
your password is : ab4
example2:
enter your password: abcde
your password is :ab5

Note: always assume that the user will enter a password with at least two characters. Additionally, you don’t need to consider the cases if the password length is an odd number (e.g: if the password length is 5 the program should only display the first two characters along with the full length).

What I have tried:

Java
import java.util.Scanner;

public class Mood {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s1 = sc.nextLine();
        int n = UserMainCode.display(s1);
        if(n==1){
         System.out.println("Valid password");
        }else{
         System.out.println("Invalid password");
        }

  }
}

class UserMainCode {
   
    
    public static int display(String password){
if(password.matches(".*[a-z]{a,}.*") && password.matches(".*[@#$]{1,}.*") && password.length()>=4 && password.length()<=20)
                    {
                                    return 1;
                    }
                    else
                    {
                                    return -1;
                    }
       }

    }
Posted
Updated 30-Sep-18 9:09am
v2
Comments
Patrice T 29-Sep-18 17:32pm    
What have you done ? Where are you stuck ?
Richard MacCutchan 30-Sep-18 3:42am    
Read the string.
Get the length of the string entered.
Divide that value by 2.
Print a substring of the original string that is the half length long.
Print the value of the length.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900