Showing posts with label java example. Show all posts
Showing posts with label java example. Show all posts

Saturday, February 18, 2012

Exception Handling Program in Java - How to Handle Exception in Java

This is sample example for how to handle exception in Java Programming. Here we handle error occurs due to array index out of bound.
Here handle error of array size exceed. In this example "exception_demo.java", array size is 10. Means we can insert element into array from arr[0] to arr[9] position. But trying to insert element at arr[10]. So ArrayIndexOutOfBoundsException occurs.

Program Code
//This program illustrates the Exception in a program.
public class exception_demo
{
 public static void main(String args[])
 {
  int []arr=new int[10];
  try
  {
   arr[10]=40; // Error occurs because arr range from 0 to 9
  }
  catch(ArrayIndexOutOfBoundsException e)
  {  
   System.out.println("Error Caught:  Array index out of bounds");
   arr[9]=40;
  }
  System.out.println("Beyond the exception point");
  System.out.println("Last element of array = "+arr[9]);
 }
}

Output

What is ArrayIndexOutOfBoundsException?

It is one of unchecked exception. Such exceptions are not listed in the list. This is identified at runtime. This occurs when array index is beyond the bounds means array size lime exceed.

Hope, this java code helps to understand exception handling in Java.

Friday, January 6, 2012

Generate Pascal Triangle in Java - Java Code for Pascal Triangle

It is a basic program to generate pascal triangle in Java language.

Pascal Triangle
It is a triangular array of the binomial coefficients in a triangle. Named Pascal comes from French mathematician, Blaise Pascal. In the program user input number. It generate rows for input number. In first row there is only the number 1. From all next rows, add the number directly above and to the left with the number directly above and to the right to find the new value.

Program Code
//Java Program to print Pascal Triangle
import java.util.*;
class Pascal 
{ 
 public static void main(String[] args) 
 { 
    System.out.print("Enter no of rows for pascal triangle: ");
    Scanner console=new Scanner(System.in);
    int num=console.nextInt();
    for (int i = 0; i < num; i++)
                  {
                        int c = 1;
 
                        for(int j = 0;j < num-i; j++)
                        {
                              System.out.print("   "); //blank space
                        }
 
                        for(int k = 0; k <= i; k++)
                        {
                              System.out.print("   ");
                              System.out.print(c); //print c value 
                              System.out.print(" ");
                              c = c * (i - k) / (k + 1);
                        }
    System.out.println();
                        System.out.println();
                  }
                  System.out.println();       
    }
}

Input/Output
Save Program at your system by name "Pascal.java".

Input
Enter value for number of rows you want in pascal triangle

Output

Hope, you like this post. :)