Friday, September 21, 2012

Unicode Character




            The char type is a primitive that holds what?
Single Unicode character which is stored in two bytes. 

The list of unicodes  are given linked below.


The unicode for your native language is linked below:

StringBuilder class

String classes create Strings that are immutable in other words when you use the String class to work with strings, the string has a fixed length and you can't edit the characters that make up the string. If you assigne a new value to a string variable, the original String object is deleted an it's replaced with a new String object that contains the new value. StringBuilder was introduced with Java 1.5 with more flexibililty and is mutable, meaning you can add, delete or replace the characters in a StringBuilder object.

http://docs.oracle.com/javase/tutorial/java/data/buffers.html

try-with-resources



Java 1.7 introduced try-with-resources statement: It uses objects that implement the java.lang.AutoCloseable interface and hence you don’t have to use finally block to close the resources.

try (RandomAccessFile in = new RandomAccessFile("file.ran", "r"))
        {           
            String s = in.readLine();
            return s;
        }
        catch (FileNotFoundException e)
        {
            System.out.println("File not found");
            System.out.println(e.toString());
            return null;           
        }
        catch (IOException e)
        {
            System.out.println("I/o error occurred");
            System.out.println(e.toString());
            return null;
        }

SSN Validator using matches()



SSNValidatorApp : You can use this to validate a textfield. Here it is SSN. Or see below for an easier method to validate.
public class SSNValidatorApp{
    public static void main(String[] args)    {
        boolean isValid =  false;
        while(!isValid)        {
        Scanner sc = new Scanner(System.in);    
        isValid = Validator.getSSN(sc, "Enter a Social Security Number: ");
        if(isValid)
            System.out.println("It is a valid SSN. Thank you");
        else
            System.out.println("It is an invalid SSN. Please try again");
        }    }  
public static boolean getSSN(Scanner sc, String prompt)    {
        System.out.print(prompt);
        String ssn = sc.nextLine();
        boolean isNumeric = false;       
            if(ssn.length() == 11)            {               
                if(getNumeric(ssn.substring(0, 3)))                {
                    if(ssn.charAt(3) == '-')                    {
                        if(getNumeric(ssn.substring(4, 5)))                        {
                            if(ssn.charAt(6) == '-')                            {
                                if(getNumeric(ssn.substring(7, 11)))                                {
                                    isNumeric = true;
                                }          }           }            }                }            }   
        return isNumeric;        }
   
    public static boolean getNumeric(String subString)    {
        boolean isNumeric = false;
        int i = 0;        
        while(i <= (subString.length()-1))        {
            char subStringLetter = subString.charAt(i);          
            switch(subStringLetter)           {
                case('0'):                  
                    isNumeric = true;
                    break;
                ………….. //put all cases  from 0 to 9 here
                case('9'):
                    isNumeric = true;
                    break;
                default:
                    isNumeric = false;                    
            }
            if(isNumeric)
                i++;
            else
                break;
        }
        return isNumeric;      
      }}}
Here is the easier way of doing it. Use matches() in Regular Expression provided by Java API. You can include the java documents in eclipse by clicking :    windows> show view> java docs if you want to see the description of the method you are using
public class SSNValidator {
        public static void main(String[] args) {
                SSNValidator ssnValidator = new SSNValidator();
                String[] ssnList = { null, "", "909-90-9900", "3423-343-23423",
"312fsdfas-sdfsaf-sadfas", "ada-dd-dasd",
                                "123-32-4532", "123*32*4532", "\t" };
                for (String ssn : ssnList) {
                        System.out.println("SSN Value [" + ssn + "] is " +
(ssnValidator.isValid(ssn) ? "valid" : "invalid") + ".");
                }
        }

        public boolean isValid(String ssn) {
                return ssn != null && ssn.matches("\\d{3}-\\d{2}-\\d{4}");

        }
}