charAt error “char cannot be converted to string”

  • currLet = str.charAt(currPos); A String value can’t be assigned to a char, they are different types, apples and oranges
  • if (currLet = str.charAt(currPos + 1)) { is actually an assignment (make currLet equal to the value of str.charAt(currPos + 1))
  • if (currLen > maxLen) { – maxLen is undefined
  • You never return anything from the method…

Try changing:

  • String currLet = ""; to something more like char currLet="\0"; and String maxLet = ""; to char maxLet="\0";
  • if (currLet = str.charAt(currPos + 1)) { to something like if (currLet == str.charAt(currPos + 1)) {
  • Add int maxLen = 0 to your variable declerations (may be under int maxCount = 0)

Now, based on your example code, public int longestRep(String str) { will need to be public static int longestRep(String str) { in order for you to call from you main method…

Leave a Comment