Archive for August 1st, 2009

Common Validators

August 1, 2009

Click http://groups.google.co.in/group/javawikiforbeginners for download utility

Contains Validation classes such as Number Validator, Phone Validator and Zip Code Validator, Test cases and Test Program. You can easily use those classes in your java code, when you need some validation on Numbers, Phone and Zip.

Jar Name : – commonValidator1_0.jar

Download jar file. unzip it and use. 

You can also plug-in this jar file in your eclipse environment, by adding an entry in build classpath of your project.

This project is under development, so you have to watch for more utilities or changes in existing utilities.

example:

import java.util.Locale;
import javawiki.com.validator.EMailValidator;
import javawiki.com.validator.NumberValidator;
import javawiki.com.validator.NumberValidatorRules;
import javawiki.com.validator.ValidatorException;
import javawiki.com.validator.ZipCodeValidator;

public class TestValidator
{
   public static void main(String arg[])
     throws ValidatorException
   {

      //Using Number Validator
      NumberValidator num = new NumberValidator();
      num.setData(“$23.78”);
      num.setDecimalPlaces(“1,2”);
      num.setValidatorRule   (NumberValidatorRules.ALLOW_DECIMAl,

                                                       NumberValidatorRules.ALLOW_DOLLAR);
      System.out.println(“Number Validator – “+num.validate());
      

      //Using ZipCode Validator
      ZipCodeValidator zipValidator = new ZipCodeValidator(new Locale(“IN”,”INDIA”));
      zipValidator.setData(“456434”);
      System.out.println(“Zip Code Validator – “+zipValidator.validate());
      

      //Using Email Validator.
      EMailValidator email = new EMailValidator();
      email.setData(“p@@acclaris.in”);
      System.out.println(“Verify Emai – “+email.validate());
   }
}

You can also refer Test cases for Zip Code Validation.

Advertisements

Reading file with Scanner

August 1, 2009

You can easily use Scanner Class to read a physical file, you can refer given example:
public class ReadFile
{
private Scanner fileScanner = null;
public ReadFile()
{
this(null);
}
public ReadFile(File fileName)
{
if(fineName==null)
{
throw new NullPointerException(“File can not be null.”);
}
fileScanner = new Scanner(fileName);
//use line separator delimiter for fetch one by one line from the file, if you don’t it fetch one by one word.
fileScanner.useDelimiter(Constant.LINE_SEP);
}
public void read()
{
while(fileScanner.hasNext())
{
System.out.println(fileScanner.next());
}
fileScanner.close();
}
public static void main(string [] arg)
{
try
{
File file = new File(arg[0])
ReadFile fileReader = new ReadFile(file);
fileReader.read();
}
catch(FileNotFoundException ex)
{
ex.printStack();
}
}
}

use this this command:
java ReadFile data.txt