I am trying to use the following code so that given a string, give the length of the longest contiguous subsequence of the same character. I am getting the error "incompatible types: char cannot be converted to java.lang.String". I have commented where the error is being found below.
public class Test {
public int longestRep(String str)
{
int currLen = 1;
String currLet = "";
String maxLet = "";
int maxCount = 0;
int currPos = 0;
int strLen = str.length();
for(currPos = 0; currPos < strLen; currPos++)
{
currLet = str.charAt(currPos); //error is on this line
if(currLet = str.charAt(currPos+1))
{
currLen++;
}
else
{
if(currLen > maxLen)
{
maxLen = currLen;
maxLet = currLet;
currLen = 1;
}
}
}
}
public static void main(String args[])
{
longestRep("AaaaMmm");
}
}
String currLet = "";tochar currLet = ' ';andString == Stringis not howStringcomparison works in Java anyway...if(currLet = str.charAt(currPos+1))-=is an assignment, not a comparison, you are trying to assign the value ofstr.charAt(currPos+1)tocurrLetwhich results in achar, butifis looking for abooleanresult ...