Saturday, January 5, 2013

How to find the largest and smallest number from a given Array using Java?

Find the largest and smallest number from a given array.

This java example shows how to find the largest and smallest number in an array.

        public class FindLargestSmallestNumber {
    
            public static void main(String[] args) {
                  
                    //array of 10 numbers
                    int numbers[] = new int[]{32,43,53,54,32,65,63,98,43,23};
                  
                    //assign first element of an array to largest and smallest
                    int smallest = numbers[0];
                    int largetst = numbers[0];
                  
                    for(int i=1; i< numbers.length; i++)
                    {
                            if(numbers[i] > largetst)
                                    largetst = numbers[i];
                            else if (numbers[i] < smallest)
                                    smallest = numbers[i];
                          
                    }
                  
                    System.out.println("Largest Number is : " + largetst);
                    System.out.println("Smallest Number is : " + smallest);
            }
    }

NumberFormatException and Exception hierarchy.

NumberFormatException and Exception hierarchy.
When we catch an exception with catch clause, we should catch exception from narrow to wide. Otherwise compiler will give a compilation error.
Here is a simple example to illustrate the NumberFormatException and usage of Exception hierarchy.

class NumberFormat{
    void throwExcep(){
        String s=null;
        int [] val={1,2,3,4,5};
        String ss="ABCD";
       
        try{
        int i=Integer.parseInt(ss);
        }
        catch(NumberFormatException np){
            System.out.println("NumberFormat");
       
        }
        catch(ArrayIndexOutOfBoundsException ae){
            System.out.println("ArrayIndexOutOfBounds");
        }
        catch(Exception e){
            System.out.println("Exception");
        }               
   
    }
    public static void main(String args[]){
        NumberFormat nf= new NumberFormat();
        nf.throwExcep();
    }
}

How to through and catch a NullPointerException in Java?

Simple example to show how to through and catch a NullPointerException in Java.

class NullPointer{
    public static void main(String args[]){
        String s=null;

            try{
                System.out.println(s.length());
            }
            catch(NullPointerException e){
                System.out.println("Null Pointer Exception caught by code");
            }
    }
}

How to validate a Social Security Number(SSN) using Java.

How to validate a SSN Number using Java?

SSNValidator class.

import java.util.regex.*;
class SSNValidator{
     public static void isValidSSN(String ssn) throws Exception{
 
        //Initialize reg expression for SSN.
        String expression = "^\\d{3}[- ]\\d{2}[- ]\\d{4}$";
        CharSequence inputStr = ssn;
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(inputStr);   
       
            if(matcher.matches()){
            System.out.println("Correct SNN Number");
            }
            else{
                throw new MyException(ssn);
            }           
    }
    public static void main(String args[])throws Exception{   
                String cmd="";
                try{
                     cmd=args[0];
                    }
                    catch(ArrayIndexOutOfBoundsException ae){
                    System.out.println("You shoud input your SSN number");
                    }
                isValidSSN(cmd);               
    }
}

Creating my own Exception class by extending Exception class.
class MyException extends Exception{
    private String s="";
    MyException(String val){
        this.s=val;
   
    }
      public String toString(){
        return "SSN number you were enter" + " " +s+" is not valid. "+ "please try again with correct format as given here. Ex:123-44-5678";
    }
}
  
How to run?
  1. Copy both java sources into one folder.
  2. Open cmd and navigate to that folder.
  3. Compile  main class(SSNValidator). 
  4. Then run with the SSN number. Ex(java SSNValidator 123-67-7890).
Meaning of Regex expression:
        "^\\d{3}[- ]\\d{2}[- ]\\d{4}$";
        ^\\d{3}:  Starts with three numeric digits.
        [- ]:  Followed by an "-"
        \\d{2}: Two numeric digits after the "-"
        [- ]:  contain an second "-" character.
       \\d{4}: ends with four numeric digits.
Above Regex format accepts only following format "xxx-xx-xxxx".
If you would like to change the Regex expression to accept 9 digits regardless of above format.
String expression = "^\\d{3}[- ]?\\d{2}[- ]?\\d{4}$"; 
This expression can accept the format given below.
SSN format xxx-xx-xxxx, xxxxxxxxx, xxx-xxxxxx; xxxxx-xxxx:
     
     ^\\d{3}: Starts with three numeric digits.
    [- ]?: Followed by an optional "-"
    \\d{2}: Two numeric digits after the optional "-"
    [- ]?: May contain an optional second "-" character.
    \\d{4}: ends with four numeric digits.


Sunday, October 7, 2012

In java, How to print reversely in a given String?

In java, How to print reversely in a given String?

class ReveseString{
public static void main(String args[]){
String s="String is immutable";
for(int i=s.length()-1; i>=0; i--){
System.out.print(s.charAt(i));
}
}
}

How to write a program in java to display given certain pattern?

How to write a program in java to display given certain pattern?

1
12
123
1234
12345
 
class Example5{
public static void main(String args[]){
 for(int i=1;i<6;i++){
System.out.println();
 for(int j=1; j<=i; j++){
System.out.print(j);
 }
}
}
}


Friday, October 5, 2012

How to Extend the Volume/Size of the C drive in Windows 7?

How to Extend the Volume/Size of the C drive in Windows 7?

1.Right click on my computer and select manage



2.Next Click Storage< Disk Management
You will be see something similar to this image.



3.You sould have free space near to C drive. for example if you have D drive adjacent to C drive. You should backup the data of D drive to another Drive and need to be delete the D drive.

4.Then Right click on C drive. you can see "extend voulme" option in the panel. If your "extend volume" option disable in that panel that means you don't have adjacent drive with free sapce. So you should do the step 3.

5.  After that You will be asked  for how much amount you need to allocate for the C drive. Select the Directions as what you want. Finally you have done.