Wednesday, 15 July 2015

Java Programs Made Easy

Q)...An arraylist of Strings is given as input. The count of the String elements that are not of size input2 string is to be returned.
input1: {"aaa","bb","cccc","d"}
input2: "bb"
output: 3("bb"-length:2)

  1.  
  2. package Set1;
  3. import java.util.*;
  4. public class ClassSet28 {
  5.  public static int StringsNotOfGivenLength(List<String> l1,String s1){
  6.   int n1=s1.length();
  7.   int c=0;
  8.   for(int i=0;i<l1.size();i++){
  9.    int n2=l1.get(i).length();
  10.    if(n1!=n2)
  11.     c++;
  12.   }
  13.   return c;
  14.  }
  15.  public static void main(String[] args) {
  16.   Scanner s=new Scanner(System.in);
  17.   System.out.println("enter the no.of elements:");
  18.   int n=s.nextInt();
  19.   List<String> l1=new ArrayList<String>();
  20.   for(int i=0;i<n;i++)
  21.    l1.add(s.next());
  22.   System.out.println("enter the input string:");
  23.   String s1=s.next();
  24.   System.out.println(StringsNotOfGivenLength(l1,s1));
  25.  }
  26. }