Wednesday, December 24, 2014

Palindrome Number descending order using comparator in java



Read Number from Console and Check if it is a Palindrome Number
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputPalindromeNumberExample
{
public static void main(String[] args)
{
System.out.println("Enter the number to check..");
int number = 0;
try
{      
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
number = Integer.parseInt(br.readLine());
}
catch(NumberFormatException ne)
{
System.out.println("Invalid input: " + ne);
System.exit(0);
}
catch(IOException ioe)
{
System.out.println("I/O Error: " + ioe);
System.exit(0);
}
System.out.println("Number is " + number);
int n = number;
int reversedNumber  = 0;
int temp=0;
while(n > 0)
{
temp = n % 10;
n = n / 10;
reversedNumber = reversedNumber * 10 + temp;
}
if(number == reversedNumber)
System.out.println(number + " is a palindrome number");
else
System.out.println(number + " is not a palindrome number");
   }
}

Simple For loop

public class SimpleForLoopExample
{
public static void main(String[] args)
{
for( ; ; )
for(int index = 0; index < 5 ; index++)
System.out.println("Index is : " + index);
for(int index=0; index < 5 ; index++)
{
System.out.println("Index is : " + index);
index++;
    }
  }
}

Sort Java Vector in descending order using comparator

import java.util.Vector;
import java.util.Collections;
import java.util.Comparator;
public class SortVectorInDescendingOrderExample
{
public static void main(String[] args)
          {
Vector v = new Vector();
v.add("1");
v.add("2");
v.add("3");
v.add("4");
v.add("5");
Comparator comparator = Collections.reverseOrder();
                    System.out.println("Before sorting Vector in descending order : " + v);
Collections.sort(v,comparator);
System.out.println("After sorting Vector in descending order : " + v);
   }
}

No comments:

Post a Comment