Results 1 to 20 of 28
Thread: Simple password validation.
- 03-20-2011, 10:17 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
Simple password validation.
Hello,
I am currently enrolled in a low level java class in college as a new computer science major.
I was wondering about this basic program.
The goal is to make a program which checks a user's password for 2 things: that it contains 8 characters and that it has a #,$, or %
Here's what I have so far.
/*
* Validates a new password
* Parameters: must be 8 characters
* Must contain $ % #
*/
------------------------
import java.util.scanner
public class CS1_Frazer_X7_2
{
public static void main(String[] args)
{
String input;
System.out.println("Please enter new password");
Scanner kbd = new Scanner;
input = kbd.nextLine();
-----------------------
Those, of course, of the obvious first steps... but what should I do next?
- 03-20-2011, 10:27 PM #2
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
input.length()
charAt(int)
- 03-20-2011, 10:38 PM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Yes, now you want to do the actual validation on the input string. You can find the length of a string with the yourString.length();
And charAt lets you see what the char at the position supplied is, so charAt(0) would give you the first item in the string. Now you need to use some sort of statement to go through each item and check if they are one of the items you need to be in the password.
- 03-20-2011, 10:44 PM #4
The indexOf method would be more useful than charAt method.
- 03-20-2011, 10:57 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
Okay then I assume just an if else statement?
So
If (input.length() >= 8) {
System.out.println("Your password has been accepted");
} else {
System.out.println("Make a new password");
}
Right?
- 03-20-2011, 11:06 PM #6
You are on the right track, however you have to check more than just the length. It might help to use a boolean and several if statements. If the length is less than 8 set the boolean to false. Also do the checks for the required chars. Then at the end if the boolean is true then display the "OK" message else the bad message.
- 03-20-2011, 11:09 PM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Yes, but you need to also test for the symbols. indexOf is the way to go, it takes an argument of string and returns a positive number if the item is found, if it isn't it returns a -1.
- 03-20-2011, 11:11 PM #8
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
Okay what I don't quite understand is how to make the indexOf thing work.
What I have been doing is
if (input.length >= 8) && (input.indexOf('#','$','%')) {
System.out.println("Password Accepted");
} else {
System.out.println("Your password is invalid. Please try again");
}
Is there some way to make that a boolean so that if it returns true just that the thing is there, it goes on to the next part?Last edited by DarkAlex; 03-20-2011 at 11:14 PM.
- 03-20-2011, 11:19 PM #9
The indexOf method only takes one parameter. You will need to call it 3 times.
- 03-20-2011, 11:22 PM #10
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
- 03-20-2011, 11:23 PM #11
Yes by using the or symbol (just like you did with and) in between the three calls to indexOf. But this can get messy real quick. That is why I suggested using several if statements, one for each check.
- 03-20-2011, 11:24 PM #12
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Yes there is ||
- 03-20-2011, 11:30 PM #13
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
import java.util.Scanner;
public class CS1_Frazer_X7_2
{
public static void main(String[] args)
{
String input;
int length;
System.out.println("Please enter new password");
Scanner k = new Scanner(System.in);
input = k.nextLine();
if (input.indexOf('$')==1) {
System.out.println("Password Approved");
} else {
System.out.println("Password Denied");
if (input.indexOf('%')==1) {
System.out.println("Password Approved");
}else{
System.out.println("Password Denied");
if (input.indexOf('#')==1) {
System.out.println("Password approved");
}else{
System.out.println("Password Denied");
if (input.length() >= 8){
System.out.println("Password Approved");
} else {
System.out.println("Password Denied");
}
}
}
}
}
}
-------------------
I'm sure you see the problems... but how do I solve them?
- 03-20-2011, 11:34 PM #14
A password of 1234567$ would not pass. Hmmmm!
Make sure your test you code extensively with as many different combinations as you can think of.
- 03-20-2011, 11:36 PM #15
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
- 03-20-2011, 11:38 PM #16
I just realised that the checks must be done in an if/else if statement. Otherwise a password of 1234567$ would pass the $ check but then fail the # check.
- 03-20-2011, 11:40 PM #17
- 03-20-2011, 11:43 PM #18
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
Sorry but I honestly don't understand exactly what you're saying.
I'm assuming if you're saying "a pass in boolean is 1 and a fail is -1" that I should do "input.indexOf("#")==1"
I know that boolean means true/false statement but that's all I know about it.
If you could dumb down everything you're saying to about 1 month java experience I might get it better haha
- 03-20-2011, 11:46 PM #19
No.
The indexOf method returns the position in the String where the target char is located or -1 if it is not found. EG in the String "hello world" the indexOf('w') call would return 6, the indexOf('h') call would return 0 and the indexOf('z') call would return -1.
- 03-20-2011, 11:48 PM #20
Similar Threads
-
user name and password validation
By exose in forum New To JavaReplies: 6Last Post: 01-20-2011, 02:59 PM -
Password
By Adomini in forum New To JavaReplies: 3Last Post: 09-20-2010, 10:43 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