java - 字符错误 "char cannot be converted to string"

标签 java compiler-errors

我正在尝试使用以下代码,以便给定一个字符串,给出相同字符的最长连续子序列的长度。我收到错误“不兼容的类型:char 无法转换为 java.lang.String”。我在下面评论了发现错误的地方。

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.charAt(int) 返回一个字符。但是 currLetString 类型,所以不能给字符赋值。使用 currLet = Character.toString(str.charAt(currPos)); 代替。

关于java - 字符错误 "char cannot be converted to string",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29270624/

相关文章:

java - Java : Cannot make static reference to non static method [duplicate]

c++ - 将 vector 作为参数传递时没有匹配的函数异常(C++)

java - 为java创建WebSphere管理客户端程序

java - ServiceLoader 不加载新服务

java - 如何a=3和b=4?

gcc - 在 CentOS - 6.X 上编译 Mono 源代码树时出错

ios - NSInvalidArgumentException : unrecognized selector sent to instance

java - 如何同时发送捆绑的卡片?

java - JAI ImageIO-核心编解码库源码

Android Firebase Unity 有错误 : Gradle failed to fetch dependencies and Resolution failed , DllNotFoundException : FirebaseCppApp-5. 2.0