The Artima Developer Community
Sponsored Link

Java Answers Forum
String check

2 replies on 1 page. Most recent reply: May 29, 2003 3:08 PM by Kishori Sharan

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 2 replies 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:07 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;
	}
}

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: String check Posted: May 29, 2003 3:08 PM
Reply to this message Reply
You can do your validation using regular expression as follows. String pattern validation is made very easy if use regular expressions. Note that regular expression is available in JDK1.4.

public class Test {
 
	public static void main (String[] args) throws Exception {
 
 
		String str[] = { "Hello", "abc_y2u_2", "_", "  ", "ab-hy", "hj^lk_90*" } ;
 
		for ( int i = 0 ; i < str.length; i++ ) {
 
			System.out.println ( "\"" + str[i] + "\" is " + isValidPassword ( str[i] ) ) ;
		}
	}
 
	public static boolean isValidPassword ( String pass ) {
		String regex = "[A-Za-z0-9_]+";
 
		boolean valid = false;
 
		if ( pass != null ) {
			valid = pass.matches ( regex );
		}
 
		return valid ;
 
	}
 
}

Flat View: This topic has 2 replies on 1 page
Topic: Swing application - pizzeria Previous Topic   Next Topic Topic: File & directory question

Sponsored Links



Google
  Web Artima.com   

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