java - Trying to return true if all the letters in a string are the same -


what have far:

 public boolean allsameletter(string str) {   (int = 1; < str.length(); i++)     {         int charb4 = i--;         if ( str.charat(i) != str.charat(charb4))         {         return false;         }          if ( == str.length())         {         return true;         }     }  } 

please excuse inefficiencies if any; still relatively new coding in general. lacking knowledge in terms of using operators , .charat() together? illogical? or error elsewhere?

you can follow below steps:

(1) first character (i.e., 0th index)

(2) check first character same subsequent characters, if not return false (and comes out method)

(3) if chars match i.e., processing goes till end of method , returns true

  public boolean allsameletter(string str) {   char c1 = str.charat(0);   for(int i=1;i<str.length;i++) {       char temp = str.charat(i);       if(c1 != temp) {          //if chars not match,           //just return false here itself,          //there no need verify other chars          return false;       }   }   //as did not return above if (inside for)   //it means, chars matched, return true   return true; } 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -