Click here to Skip to main content
15,886,798 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
write a Boolean method which gets a string and verify if it has at least 10 character , one uppercase , one lowercase , one number, and only alphanumeric, and return Boolean true or false , in the main method we can check the answer

What I have tried:

Java
import java.util.Scanner;
public class PasswordValidator {
  public static void main (String[] args){
    Scanner input= new Scanner (System.in);
    //System.out.println
      boolean answer1 =(isSecurePassword( input.nextLine()));
      System.out.println(answer1);
    /*String pass1 = "This is a secure password";
    String pass2 = "This is not a secure password";
    
    boolean answer1 = isSecurePassword(pass1);
    boolean answer2 = isSecurePassword(pass2);
    
    System.out.println("answer1 = " + answer1);
    System.out.println("answer2 = " + answer2);*/

    
  }
  public static boolean isSecurePassword (String password){
  
   
    int i;
    for (i=0;i<password.length();i++){
      
    if (i!=10)
    {
   
    return false;
    
    }
    
      
  }
  
  }
}
Posted
Updated 13-Feb-18 22:42pm
v3

Read the question: it's makes it pretty obvious what you have to do.
1) Read in the string.
2) Check it has 10 characters. If it doesn't, return false.
3) Declare three boolean variables: hasUpperCase, hasLowerCase, hasDigit and set them all to false.
4) Loop through the string, looking at each character in turn.
4.1) If it's Uppercase, set hasUpperCase to true.
4.2) Otherwise, if it's lowercase, set hasLowerCase to true.
4.3) Otherwise, if it's a digit, set hasDigit to true.
4.4) Otherwise, return false;
5) After the loop, return hasUpperCase AND hasLowerCase AND hasDigit
 
Share this answer
 
Comments
Member 13676546 13-Feb-18 13:13pm    
thanks for the explanation . when I compile the first part which is check if it has 10 character it gives a message that it must return Boolean type .. so I still stuck in the fist part of the question..
OriginalGriff 13-Feb-18 14:00pm    
That's because you aren't doing what I suggested: look at your code and mentally follow it through (or use the debugger) - it's fundamentally flawed if you look at all closely.
OriginalGriff 14-Feb-18 5:58am    
I was going to say "what errors?" but then I read the code ... oh dear ...

Stop guessing. Start thinking. Go back to what I originally said, and read it again. Is that what you are doing?
Hint: https://stackoverflow.com/questions/2451650/how-do-i-apply-the-for-each-loop-to-every-character-in-a-string
Member 13676546 14-Feb-18 8:32am    
thanks for the link, however, I'm beginner, and I just learnt conditional loop and a bit of string very basics.. not anything else, the goal of the exercise is to write a method and call it. we can not use scanner in the method but we ca verify the result ( which should be Boolean type) through the main method. so I need to write a program to validate a password with the basics.
OriginalGriff 14-Feb-18 8:39am    
You need to access each character individually to find out what kind of character it is: that means a loop and either a "for each" construct or an array index - as the link shows.
I suggest that you look back through your course notes and see what loops you have been introduced to: without one you can't do the exercise so I suspect you have just skipped over them when you were in class for some reason.
Yor implementation (the one in the comment) is full of bugs. Hopefully I've fixed all of them. Try
Java
import java.util.Scanner;

public class PasswordValidator
{
  public static void main (String[] args)
  {
    Scanner input = new Scanner (System.in);
    boolean isSecure =isSecurePassword( input.nextLine());
    System.out.println(isSecure);
  }

  public static boolean isSecurePassword (String password)
  {
    if ( password.length() < 10) return false;

    boolean uppercase=false;
    boolean lowercase=false;
    boolean digit=false;

    for(char c : password.toCharArray())
    {
      if (c >='a'&& c <='z')
        lowercase = true;
      else if (c >='A'&& c <='Z')
        uppercase =true;
      else if ( c >='0' && c<='9')
        digit = true;
    }
    return (uppercase && lowercase && digit);
  }
}
 
Share this answer
 
v2
Comments
Member 13676546 14-Feb-18 8:37am    
thanks a lot for debugging it ( is it the correct to say like this ?) the only thing is I cannot use array.
CPallini 14-Feb-18 9:23am    
You are welcome.
Member 13676546 14-Feb-18 9:01am    
how can we rewrite it without using array ?
CPallini 14-Feb-18 9:23am    
for(char c : password.toCharArray())

is the same as
for (int i = 0; i < password.length(); ++i)
{
  char c = password.charAt(i);
Member 13676546 14-Feb-18 9:31am    
thanks again for the explanation. it was clear and helpful.

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