The Artima Developer Community
Sponsored Link

Java Answers Forum
String check

1 reply on 1 page. Most recent reply: May 29, 2003 12:08 PM by Jaycee

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Sujesh.S.Nair

Posts: 3
Nickname: deepu
Registered: May, 2003

String check Posted: May 29, 2003 3:18 AM
Reply to this message Reply
Advertisement
hi,
I have a problem .pls do help.

I read a string from the user which is a password.I reqiure to check if the characters in the input has only characters of A-Z,a-Z,0-9 & underscore.how do i proceed.
thanx
sujesh


Jaycee

Posts: 26
Nickname: jaycee
Registered: Apr, 2003

Re: String check Posted: May 29, 2003 12:08 PM
Reply to this message Reply
import java.util.HashMap;
 
public class PasswordValidator{	
	private static HashMap _legal_chars;
	
	static{
		String[] legalLetters = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
		String[] legalNumbers = {"0","1","2","3","4","5","6","7","8","9"};
		
		_legal_chars = new HashMap();
		String character;
		
		for(int i=legalLetters.length-1; i>=0; i--){
			character = legalLetters[i];
			_legal_chars.put(character,null);
			character = character.toUpperCase();
			_legal_chars.put(character,null);
		}
		
		for(int i=legalNumbers.length-1; i>=0; i--){
			character = legalNumbers[i];
			_legal_chars.put(character,null);
		}
		
		_legal_chars.put("_",null);
	}
	
	public static boolean isPasswordValid(String password){
		if(password==null){
			throw new IllegalArgumentException("password cannot be null");
		}
		
		boolean isValid = true;
		char[] chars = password.toCharArray();
		
		for(int i=chars.length-1;i>=0; i--){
			if(!_legal_chars.containsKey(String.valueOf(chars[i]))){
				isValid = false;
				break;
			}
		}
		
		return isValid;
	}
}

Flat View: This topic has 1 reply on 1 page
Topic: File & directory question Previous Topic   Next Topic Topic: how can i call one applet with another??

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use