Results 1 to 9 of 9
  1. #1
    AB1826 is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default Need help for password check program

    Hi!
    We are doing a password check program in school and the program needs to ask you to type in a password and then it needs to eveluate if the password has Capital and smal letters, a digit and an non-alphanumeric or special character.

    Now we have pretty much done everything, but as you see in the end where we have put the last else and the word "FEL" which means "wrong" in swedish, we get it looped all the time even if we have a right or wrong password

    Here is my program:
    PHP Code:
    import java.util.*;
    import java.lang.*;
    
    
    public class PassCheck{
    
    
    public static void main (String [] str){
    
    Scanner scan = new Scanner(System.in);
    
    System.out.println("Ange lösenord: ");
    
    String losen = scan.nextLine();
    
    
    boolean number = false;
    boolean uppercase = false;
    boolean lowercase = false;
    boolean specialsign = false;
    
    
    for (int i = 0; i < losen.length(); i++){
    
    if ( losen.length() <= 7){
    
    System.out.print("FEL PW FÖR KORT");
    break;
    }
    else {
    
    if ( Character.isDigit(losen.charAt(i))){
    
    number = true;
    }
    
    if ( Character.isUpperCase(losen.charAt(i))){
    
    uppercase = true;
    }
    
    if ( Character.isLowerCase(losen.charAt(i))){
    
    lowercase = true;
    }
    if ( !Character.isLetterOrDigit(losen.charAt(i))){
    
    specialsign = true;
    }
    
    if (number == true && uppercase == true && lowercase == true && specialsign == true){
    
    System.out.print("LOS OK");
    break;
    
    else
    System.out.println("FEL");
    }
    
    }
    }
    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Need help for password check program

    First off, it is never necessary to import java.lang.* -- that package is automatically imported.

    Without proper indentation, it's difficult to see where each of your program flow structures (if/else blocks and for loop) end. In fact, at a quick glance it looks like it might not even compile -- but I'm likely reading it wrong.

    See Code Conventions for the Java Programming Language: Contents then re-post your code with consistent indentation of 2 to 4 spaces (don't use tabs, most forums don't display them well).
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    AB1826 is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default Re: Need help for password check program

    Hi! Thank you for your replying!
    Ok, from what i understood, because i am new to Java, you meant that it is hard to read my code.
    So i have tried to simplify it.
    I hope it easier to read it now.
    I have also switch some words that where in swedish into english and i have removed the lang utility.

    Here is the code:

    PHP Code:
    import java.util.*;
    
    
    
    public class PassCheck{
    
    
    public static void main (String [] str){
    
    Scanner scan = new Scanner(System.in);
    
    System.out.println("Enter the password: ");
    
    String losen = scan.nextLine();
    
    
    boolean number = false;
    boolean uppercase = false;
    boolean lowercase = false;
    boolean specialsign = false;
    
    
    for (int i = 0; i < losen.length(); i++){
    
    if ( losen.length() <= 7){
        System.out.print("Password is to short");
        break;
    }
    else 
    {
    
        if ( Character.isDigit(losen.charAt(i))){
        number = true;
    }
    
                
    				
    				if ( Character.isUpperCase(losen.charAt(i))){
                uppercase = true;
    }
    
                         
    							
    							if ( Character.isLowerCase(losen.charAt(i))){
                         lowercase = true;
    }
                                 
    									  
    									  
    									  if ( !Character.isLetterOrDigit(losen.charAt(i))){
                                 specialsign = true;
    }
    
    
    
    if (number == true && uppercase == true && lowercase == true && specialsign == true){
        System.out.print("Password OK");
        break;
    }
    
    else
        System.out.print("Invalid password");
    
    
    
    
    
    }
    }
    }
    }

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Need help for password check program

    See Code Conventions for the Java Programming Language: Contents then re-post your code with consistent indentation of 2 to 4 spaces (don't use tabs, most forums don't display them well).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    mkarthik90 is offline Member
    Join Date
    Feb 2012
    Posts
    62
    Rep Power
    0

    Default Re: Need help for password check program

    You Should use losen.length and not losen.length().

    Its because you dont have a length function in the String class. You have only length variable which specifies the length of the String.

    Change the .length() to .length.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Need help for password check program

    Quote Originally Posted by mkarthik90 View Post
    You Should use losen.length and not losen.length().

    Its because you dont have a length function in the String class. You have only length variable which specifies the length of the String.

    Change the .length() to .length.
    Please don't post nonsense here. Do you know how to find the API? Do you know how to read it?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    mkarthik90 is offline Member
    Join Date
    Feb 2012
    Posts
    62
    Rep Power
    0

    Default Re: Need help for password check program

    Do you want to make sure that the password needs to contain atleast one special character small and uppercase.
    If anyone is enough u can use this code i have made the modification .





    import java.util.*;
    public class PassCheck
    {
    public static void main (String [] str)
    {
    Scanner scan = new Scanner(System.in);
    System.out.println("Ange lösenord: ");
    String losen = scan.nextLine();
    boolean number = false;
    boolean uppercase = false;
    boolean lowercase = false;
    boolean specialsign = false;
    for (int i = 0; i < losen.length(); i++)
    {
    if ( losen.length() <= 7)
    {
    System.out.print("FEL PW FÖR KORT");
    break;
    }
    else
    {
    if ( Character.isDigit(losen.charAt(i)))
    {
    number = true;

    }
    if ( Character.isUpperCase(losen.charAt(i)))
    {
    uppercase = true;

    }
    if ( Character.isLowerCase(losen.charAt(i)))
    {
    lowercase = true;

    }
    if ( !Character.isLetterOrDigit(losen.charAt(i)))
    {
    specialsign = true;

    }
    if (number == true || uppercase == true || lowercase == true || specialsign == true)
    {
    System.out.print("LOS OK");

    }
    else
    System.out.println("FEL");
    }
    }
    }
    }


    Just change the AND condition to OR condition.

  8. #8
    mkarthik90 is offline Member
    Join Date
    Feb 2012
    Posts
    62
    Rep Power
    0

    Default Re: Need help for password check program

    And to make sure that your code contains atleast one lowercase , one uppercase , one digit , one special character.
    Use the below


    import java.util.*;
    public class PassCheck
    {
    public static void main (String [] str)
    {
    Scanner scan = new Scanner(System.in);
    System.out.println("Ange lösenord: ");
    String losen = scan.nextLine();
    boolean number = false;
    boolean uppercase = false;
    boolean lowercase = false;
    boolean specialsign = false;
    int a=0,b=0,c=0,d=0,e=0;
    for (int i = 0; i < losen.length(); i++)
    {
    if ( losen.length() <= 7)
    {
    System.out.print("FEL PW FÖR KORT");
    break;
    }
    else
    {
    if ( Character.isDigit(losen.charAt(i)))
    {
    number = true;
    a++;
    }
    if ( Character.isUpperCase(losen.charAt(i)))
    {
    uppercase = true;
    b++;
    }
    if ( Character.isLowerCase(losen.charAt(i)))
    {
    lowercase = true;
    c++;
    }
    if ( !Character.isLetterOrDigit(losen.charAt(i)))
    {
    specialsign = true;
    d++;
    }
    if (number == true || uppercase == true || lowercase == true || specialsign == true)
    {
    System.out.print("LOS OK");
    }
    else
    System.out.println("FEL");
    }
    }


    if(a>0 && b>0 && c>0 && d>0)
    {
    System.out.println("your password contains atleast one digit and uppercase and lowercase and specialcharacter");
    }
    }

    }

  9. #9
    mkarthik90 is offline Member
    Join Date
    Feb 2012
    Posts
    62
    Rep Power
    0

    Default Re: Need help for password check program

    @Darry
    Sorry darry i dont know but i settled the issue.. And sry for posting the the wrong thing.

Similar Threads

  1. Simple Password Check problem
    By Gsangha in forum New To Java
    Replies: 1
    Last Post: 12-26-2011, 06:15 AM
  2. my password program
    By zneith in forum New To Java
    Replies: 2
    Last Post: 01-12-2009, 06:14 AM
  3. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum New To Java
    Replies: 2
    Last Post: 11-14-2008, 08:53 PM
  4. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 0
    Last Post: 11-14-2008, 02:22 PM
  5. How to check password of a jsp/html with the password of Database(mysql) #1
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 2
    Last Post: 11-14-2008, 02:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •