Showing posts with label ArrayList. Show all posts
Showing posts with label ArrayList. Show all posts

Saturday, 18 June 2016

Program to print the Words of a Given String ending with a Particular Letter.

Explanation :
If String= "All cars are supercars until they have tires".
1)If Letter = "s" then output will be "cars,supercars,tires".
2)If Letter = "l" then output will be "All,until".
3)If letter = "v" then output will be "There are no words ending with Letter v".

Code:

import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;
public class EndsWithLetter
{   
     public static void main(String args[])
     {
        int count=0;
        ArrayList<String> ar= new ArrayList<String>();
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter any String");
        String x=sc.nextLine();
        System.out.println("Enter a letter");
        String y=sc.nextLine();
        StringTokenizer st=new StringTokenizer(x,",/-@ ");
        while(st.hasMoreTokens())
        {
            String st1 = (String) st.nextToken();
            if(st1.endsWith(y))
            {
                ar.add(st1);//Adding Words in ArrayList
                count+=1;
            }
        }
        if(count!=0)
        {
                System.out.println("Words ending with letter "+y+" are:");
                for(String z:ar)
                {
                    System.out.println(z);
                }
        }
        else System.out.println("There are no words ending with Letter "+y);
        sc.close();
    }
}

Friday, 17 June 2016

Program to find the Lucky number of a Person based on Date Of Birth.

Explanation :
If Input is 15-Mar-1995 then the output will calculated as :
(1+5)+(0+3)+(1+9+9+5)=33 which will proceed to give output as 3+3=6 which is the Lucky Number.

Code:

import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;

public class LuckyNumber
{
      public static void main(String[] args)
      {
         int n,rem,sum = 0;
         ArrayList<String> s=new ArrayList<String>();
         String s2;
         int date,year,month;
        
         //Taking Date as Input
         Scanner sc=new Scanner(System.in);
         System.out.println("Enter the Date in the 15-Mar-1995 Format:");
         String day=sc.nextLine();
         StringTokenizer st=new StringTokenizer(day,",/- ");
         while(st.hasMoreTokens())
         {
             s2 =st.nextToken();
             s.add(s2);//Adding Date In the ArrayList as String Objects
         }
         date=Integer.parseInt(s.get(0));//Getting the Date
         s2=s.get(1);//Getting the Month from the Date
         s2.toLowerCase();
         year=Integer.parseInt(s.get(2));//Getting Year
        
         if(s2=="jan") month =1;
         else if(s2=="feb") month =2;
         else if(s2=="mar") month =3;
         else if(s2=="apr") month =4;
         else if(s2=="may") month =5;
         else if(s2=="jun") month =6;
         else if(s2=="jul" || s2=="july") month =7;
         else if(s2=="aug") month =8;
         else if(s2=="sep" || s2=="sept") month =9;
         else if(s2=="oct") month =10;
         else if(s2=="nov") month =11;
         else  month =12;
        
         n=date+month+year;
        
         while(n > 0)
         {
                     while(n != 0)
                     {
                          rem = n%10;
                          sum = sum+rem;
                          n=n/10;
                     }
                     if(sum > 9)
                     {
                            n = sum;
                            sum = 0;
                     }
          }
          System.out.println("Your Lucky Number is: "+sum);  
          sc.close();
    }  
}