Consecutive sequenced numbers in a string | Java Program to check whether a String contains consecutive sequential numbers or not ; function to check consecutive sequential number ; variable to store starting number ; length of the input String ; find the number till half of the String ; new String containing the starting substring of input String ; converting starting substring into number ; backing up the starting number in start ; while loop until the new_String is smaller than input String ; next number ; concatenate the next number ; check if new String becomes equal to input String ; if String doesn 't contains consecutive numbers ; Driver Code
class GFG { static int isConsecutive ( String str ) { int start ; int length = str . length ( ) ; for ( int i = 0 ; i < length / 2 ; i ++ ) { String new_str = str . substring ( 0 , i + 1 ) ; int num = Integer . parseInt ( new_str ) ; start = num ; while ( new_str . length ( ) < length ) { num ++ ; new_str = new_str + String . valueOf ( num ) ; } if ( new_str . equals ( str ) ) return start ; } return - 1 ; } public static void main ( String [ ] args ) { String str = "99100" ; System . out . println ( " String : ▁ " + str ) ; int start = isConsecutive ( str ) ; if ( start != - 1 ) System . out . println ( "Yes NEW_LINE" + start); else System . out . println ( " No " ) ; String str1 = "121315" ; System . out . println ( " String : " + str1); start = isConsecutive ( str1 ) ; if ( start != - 1 ) System . out . println ( "Yes NEW_LINE" + start); else System . out . println ( " No " ) ; } }