Wednesday, 15 July 2015

Java Programs Made Easy

Q)... find the three consecutive characters in a string. if the string consists any three 
   consecutive characters return 1 else return -1
   like AAAxyzaaa will return true.


  1. package Set1;
  2. public class ClassSet36 {
  3. public static void main(String[] args) {
  4.  String s1="aaaaxyzAAA";
  5.  boolean b=consecutiveCharactersCheck(s1);
  6.  if(b==true)
  7.   System.out.println("presence of consecutive characters");
  8.  else
  9.   System.out.println("absence of consecutive characters");
  10.  }
  11. public static boolean consecutiveCharactersCheck(String s1) {
  12.  boolean b=false;
  13.  int c=0;
  14.  for(int i=0;i<s1.length()-1;i++)
  15.   if((s1.charAt(i+1)-s1.charAt(i))==1)
  16.     c++;
  17.  if(c>=2)
  18.   b=true;
  19.  return b;
  20. }
  21. }