3

What I have so far:

 public boolean allSameLetter(String str)
{
  for (int i = 1; i < str.length(); i++)
    {
        int charb4 = i--;
        if ( str.charAt(i) != str.charAt(charb4))
        {
        return false;
        }

        if ( i == str.length())
        {
        return true;
        }
    } 
}

Please excuse any inefficiencies if any; still relatively new to coding in general. Am I lacking some knowledge in terms of using operators and .charAt() together? Is it illogical? Or is my error elsewhere?

Harry You
  • 87
  • 2
  • 3
  • 11
  • what are you trying to achieve exactly? comparing two strings? – Khalil M Nov 13 '16 at 01:22
  • One thing: name your variables completely, don't abbreviate them. You're decreasing `i` so you enter an infinite loop... – Andrew Li Nov 13 '16 at 01:24
  • Having the code check a string to see if its composed of the same letter. (ie. "aaa", "oooo", "ddddd") – Harry You Nov 13 '16 at 01:25
  • Related post - [Determine if all characters in a string are the same](https://stackoverflow.com/q/16027475/465053) – RBT Jun 10 '18 at 13:46

7 Answers7

6

Using regex:

return str.matches("^(.)\\1*$");

Using streams:

str.chars().allMatch(c -> c == str.charAt(0));

Other:

return str.replace(String.valueOf(str.charAt(0), "").length() == 0;
Gayan Weerakutti
  • 11,904
  • 2
  • 71
  • 68
3

You can follow the below steps:

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

(2) Check the first character is the same with subsequent characters, if not return false (and comes out from method)

(3) If all chars match i.e., processing goes till the end of the method and 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 does NOT match, 
         //just return false from here itself,
         //there is no need to verify other chars
         return false;
      }
  }
  //As it did NOT return from above if (inside for)
  //it means, all chars matched, so return true
  return true;
}
Vasu
  • 21,832
  • 11
  • 51
  • 67
2

As Andrew said, you are decreasing i within your for loop. You can fix this by changing it to int charb4 = i - 1;. As for making your code more efficient you could condense it down to this.

public boolean allSameLetter(String str) {
    for(char c : str.toCharArray())
        if(c != str.charAt(0)) return false;
    return true;
}
Matthew Wright
  • 1,485
  • 1
  • 14
  • 38
1

Comment if you don't understand a part of it :)

 public boolean allSameLetter(String str)
 {
 for (int i = 1; i < str.length() -1; i++)
  {
    if ( str.charAt(i) != str.charAt(i+1))
    {
    return false;
    }
  } 
 return true
}

-1 is there since I am checking the current value in the array, then the next value in the array, thus I need to stop a place earlier.

If the loop if statement is never entered, it will make it far enough into the code to return true

Gabriel Stellini
  • 426
  • 4
  • 13
0

You have to create a for loop that searches through the length of the String - 1. this way the program will not crash because of a 3 letter word with the program trying to get the 4th letter. This is what works for me:

public boolean allSameLetter(String str)
{
    for(int i = 0; i< str.length()-1; i++){
        if (str.charAt(i) != str.charAt(i+1)){
            return false;
        }
    }
    return true;
}
Carter Cobb
  • 790
  • 7
  • 13
0

if((new HashSet<Character>(Arrays.asList(s.toCharArray()))).size()==1) return true; return false;

This should be enough

-1

The bug is caused by

int charb4 = i--;

this line is equal to

int charb4 = i-1;
i=i-1;

Because of this, your loop will never stop.
The easiest way to fix this

public boolean allSameLetter(String str)
{
  for (int i = 1; i < str.length(); i++)
  {
     if ( str.charAt(i) != str.charAt(i-1))
     {
       return false;
     }
  } 
}
Hendrik T
  • 191
  • 7