GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Interview Questions  >  J2EE  >  Java

 Print  |  
Question:  Write a code that returns the number of names in a given array that end in either "ie" or "y"?



September 09, 2008 14:43:26 #2
 disskyz29   Member Since: August 2008    Total Comments: 1 

RE: Write a code that returns the number of names in a given array that end in either "ie" or "y"?
 

public class Names {
 public static void main (String [] args)

 {
  int count = 0;
  String names[] = {"Valerie", "Jia", "Debbie", "Harvey", "Winslow", "Suzy"  };
  for (int i = 0; i <names.length; i++)
  {
   String a = "y";
   String b = "ie";
   String result;
   result = names[i];
   boolean yChar = result.endsWith(a);
   boolean ieChar = result.endsWith(b);
   if (yChar || ieChar){
    count++;
   }
    
  }
  System.out.println("The number of names ending with ie or y is " + count);
 }
}

     

 

Back To Question