Pages

Ads 468x60px

Monday, September 23, 2013

Palindrome checker using java

import java.util.*;

public class palindrome
{
   public static void main(String args[])
   {
      String original, reverse="";
      Scanner in = new Scanner(System.in);

      System.out.println("Enter a string to check if it is a palindrome");
      original = in.nextLine();
     

      int length = original.length();

      for ( int i = length - 1 ; i >= 0 ; i-- )
         reverse = reverse + original.charAt(i);

      if (original.equals(reverse))
      {
         System.out.println("Entered string is a palindrome.");
       
      }
      else
      {
         System.out.println("Entered string is not a palindrome.");
          System.out.println(reverse);
      }

   }
}

Thursday, September 12, 2013

How to sort array values in ascending / descending order using Java

Here's the idea on how to sort array; descending or ascending order: I hope this would helps.

Sample output:



import java.util.Scanner;

public class AscendingDescending
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        int tenNums[]=new int[10], orderedNums[]=new int[10];
        int greater;
        String choice;

        System.out.println("Enter 10 integers : ");
        for (int i=0;i<tenNums.length;i++)
        {
            System.out.print(i+1+"=> ");
            tenNums[i] = scan.nextInt();
        }
        System.out.println();

        //imperfect number ordering algorithm
        for(int indexL=0;indexL<tenNums.length;indexL++)
        {
            greater=0;
            for(int indexR=0;indexR<tenNums.length;indexR++)
            {
                if(tenNums[indexL]>tenNums[indexR])
                {
                    greater++;
                }
            }
            orderedNums[greater]=tenNums[indexL];
        }

        //ask if ascending or descending
        System.out.print("Display order :\nA - Ascending\nD - Descending\nEnter your choice : ");
        choice = scan.next();

        //output the numbers based on choice
        if(choice.equalsIgnoreCase("a"))
        {
            for(greater=0;greater<orderedNums.length;greater++)
            {
                System.out.print(orderedNums[greater]+" ");
            }
        }
        else if(choice.equalsIgnoreCase("d"))
        {
            for(greater=9;greater>-1;greater--)
            {
                System.out.print(orderedNums[greater]+" ");
            }
        }
    }
}

How to remove duplicates in an array using java without using function?

Here's a simple code for removing duplicates in String array without using function or methods. I hope this would helps.



import java.io.*;
public class Arrayremoveduplicates {

    public static void main(String[] args) throws Exception {
   
    InputStreamReader reader = new InputStreamReader(System.in);
    BufferedReader scan = new BufferedReader (reader);
   
        String [] name = new String[5];
       
        System.out.println("Enter name: ");
        for (int i=0;i<name.length;i++)
        {
         
          name[i] = scan.readLine();
    }
      System.out.println("List of 5 Names :");
   for (int a = 0; a < name.length; a++)
   {
        System.out.print(name[a] + " ");
   }
        System.out.println();
        int total=0;
        System.out.println("Names without duplicates :");
        for (int i = 0; i < name.length; i++)
        {
            int duplicate = 0;
            for (int j = i + 1; j < name.length; j++)
            {
                if (name[i].equals( name[j]))
                {
                    duplicate = duplicate + 1;
                   
                }
            }
         
            if (duplicate == 0)
            {
             total++;
                System.out.println(name[i] + " ");
            }
       
        }
            System.out.println("Number of Duplicates: " + total);
    }
}