Results 1 to 9 of 9
- 02-15-2012, 12:41 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 2
- Rep Power
- 0
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"); } } } } }
- 02-15-2012, 01:11 AM #2
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).Why do they call it rush hour when nothing moves? - Robin Williams
- 02-15-2012, 02:17 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 2
- Rep Power
- 0
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"); } } } }
- 02-15-2012, 02:21 PM #4
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).
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-15-2012, 07:13 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 60
- Rep Power
- 0
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.
- 02-15-2012, 08:35 PM #6
- 02-16-2012, 05:09 AM #7
Member
- Join Date
- Feb 2012
- Posts
- 60
- Rep Power
- 0
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.
- 02-16-2012, 05:18 AM #8
Member
- Join Date
- Feb 2012
- Posts
- 60
- Rep Power
- 0
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");
}
}
}
- 02-16-2012, 05:19 AM #9
Member
- Join Date
- Feb 2012
- Posts
- 60
- Rep Power
- 0
Similar Threads
-
Simple Password Check problem
By Gsangha in forum New To JavaReplies: 1Last Post: 12-26-2011, 05:15 AM -
my password program
By zneith in forum New To JavaReplies: 2Last Post: 01-12-2009, 05:14 AM -
how to check password for 3 times enterd wrong password
By sk.mahaboobbhasha@gmail.c in forum New To JavaReplies: 2Last Post: 11-14-2008, 07:53 PM -
how to check password for 3 times enterd wrong password
By sk.mahaboobbhasha@gmail.c in forum Java ServletReplies: 0Last Post: 11-14-2008, 01:22 PM -
How to check password of a jsp/html with the password of Database(mysql) #1
By sk.mahaboobbhasha@gmail.c in forum Java ServletReplies: 2Last Post: 11-14-2008, 01:11 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks