If current number is the maximum obtained so far
if ( ( newstr . toString ( ) . compareTo ( ans . toString ( ) ) > 0 && ans . length ( ) == newstr . length ( ) ) || newstr . length ( ) > ans . length ( ) ) { ans . setLength ( 0 ) ; ans . append ( newstr ) ; } } return ; } StringBuilder x = new StringBuilder ( newstr . toString ( ) + str . charAt ( index ) ) ;